2014-09-25 2 views
1

YouTube 동영상을 재생할 때 YouTube API를 사용하고 있습니다. play()pause() 기능이 YouTube 플레이어에 있습니다. 특정 시간에 비디오를 중단해야합니다. pause()을 사용하고 있지만 비디오의 배경이 버퍼링됩니다. 특정 시간 제한 후 비디오 버퍼링을 중지하려면 어떻게합니까? 웹뷰에서 아래와 같이 버퍼하는 URL의 옵션 시작과 끝 태그,youtube 비디오를 중지하거나 android에서 youtube-api의 버퍼 제한을 설정하는 방법

https://www.youtube.com/v/K_AdxJWFUh4&start=0&end=30

참고 : 나는 유튜브 플레이어는 비디오

답변

2

확인이를 웹뷰하지 재생 사용하고 있습니다 코드

public class YouTube extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener { 

    public static final String API_KEY = "<---YOUR KEY--->"; 
    public static final String VIDEO_ID = "<--VIDEO ID YOU WANT TO PLAY--->"; 
    private static YouTubePlayer player; 

    TextView text; 

    //this is the end time in milliseconds (65th second) 
    public int endTime = 5600; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my); 
     text = (TextView) findViewById(R.id.text); 
     YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview); 
     youTubePlayerView.initialize(API_KEY, this); 
    } 

    @Override 
    public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) { 
     Toast.makeText(getApplicationContext(), 
     "onInitializationFailure()", 
     Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) { 

     MyActivity.player = player; //necessary to access inside Runnable 

     //start the video at 36th second 
     player.loadVideo(VIDEO_ID, 0); 

     final Handler handler = new Handler(); 
     handler.postDelayed(() -> { 
      //For every 1 second, check the current time and endTime 
      if(MyActivity.player.getCurrentTimeMillis() <= endTime) { 
       text.setText("Video Playing at " + MyActivity.player.getCurrentTimeMillis()); 
       handler.postDelayed(this, 1000); 
      } else { 
       handler.removeCallbacks(this); //no longer required 
       text.setText(" Reached " + endTime); 
       MyActivity.player.pause(); //and Pause the video 
      } 
     }, 1000); 
    } 
} 

레이아웃 XML :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="http://android-er.blogspot.com/" 
     android:textStyle="bold" 
     android:layout_gravity="center_horizontal" 
     android:autoLink="web" /> 

    <com.google.android.youtube.player.YouTubePlayerView 
     android:id="@+id/youtubeplayerview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

</LinearLayout>