2013-12-13 45 views
0

我正在使用YouTubeAndroidPlayer api在我的應用程序中播放YouTube視頻。我想從中間開始播放視頻,但無法找到用於(開始和結束)的方法,因爲我們可以在youtube API中找到這些方法。無法使用YouTubeAndroidPlayer api提供開始和結束值

https://www.youtube.com/v/BmOpD46eZoA?start=36&end=65 

在YouTubeAndroidPlayer api中,我有一個如下代碼來播放視頻。我無法從視頻中間開始。我沒有找到玩家的任何方法來給出開始和結束值。

我搜索了很多的解決方案,但沒有找到任何luck.Please建議。

@Override 
public void onInitializationSuccess(Provider provider, YouTubePlayer player, 
    boolean wasRestored) { 
    if (!wasRestored) { 
//  player.cueVideo(VIDEO_ID); 

     player.loadVideo(xyoajjlPt_o); 

     } 
+0

可能的重複[如何在youtube Android播放器中爲youtube視頻提供開始和結束參數?](http://stackoverflow.com/questions/20565494/how-to-provide-start-and-end-parameters爲youtube-video-in-youtube-android-p) – JoelFernandes

+0

你能爲上述問題建議嗎? – user1891910

回答

2

使用方法與timeMillis參數:

player.loadVideo (String videoId, int timeMillis) 

你的情況:

player.loadVideo("xyoajjlPt_o", 36000); 
//this will start the video from 36th second 

[編輯]

好了,你可以利用處理器來跟蹤視頻的當前運行時間。當以毫秒爲單位的時間達到您想停止視頻的時間時,只需撥打player.pause()方法即可。

下面是活動的完整代碼:

public class MyActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{ 

    public static final String API_KEY = "YOUR_API_KEY_HERE"; 
    public static final String VIDEO_ID = "yeF_b8EQcK0"; 
    private static YouTubePlayer player; 

    TextView text; 

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


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.my_activity); 
     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, 36000); 

     final Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       //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); 
    } 
} 

附:一個重複的問題被問到here,我在那裏更新了我的answer

+0

如何給出最終值和控件 – user1891910

+0

@ user1891910檢查我編輯的答案。 – JoelFernandes