2013-12-13 17 views
3

我在我的應用程序中使用Youtube Player API來播放YouTube視頻。我想從36秒開始播放視頻,並希望在65秒內結束這段視頻。如何在youtube Android播放器中爲YouTube視頻提供開始和結束參數?

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

我使用youtubeAndroidPlayer API。我沒有找到Youtubeplayer設置開始和結束值的任何方法。任何人都可以建議如何設置參數。

而且我也希望使用youtube api中的「controls」字段來隱藏控件,但是我沒有找到任何方法來隱藏它。

我的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: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> 

我的活動:

public class MainActivity extends YouTubeBaseActivity implements 
YouTubePlayer.OnInitializedListener{ 

public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA"; 
public static final String VIDEO_ID = "xyoajjlPt_o"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     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) { 
    if (!wasRestored) { 
     player.cueVideo(VIDEO_ID); 

     } 
} 

回答

3

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

下面是活動的完整代碼:

public class MyActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{ 

    public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA"; 
    public static final String VIDEO_ID = "xyoajjlPt_o"; 
    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); 
    } 
} 

我不知道是否有任何其他方式。希望它解決了你的問題。

P.S.一個重複的問題被問到here,我在那裏更新了我的answer

+0

非常感謝您的回答,我如何隱藏/顯示控件。 – user1891910

+0

@ user1891910您可以將播放器樣式設置爲「無色」。爲此,請使用:'MyActivity.player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);'並將其放入'else'部分。通讀文檔,你會很容易地得到你的答案 - https://developers.google.com/youtube/android/player/ – JoelFernandes

+0

嗨,我也使用這個Youtube api對於加載視頻,但這個api不起作用,最新版本4.0以上當我嘗試加載視頻它顯示intializer錯誤可以請你建議我笏我可以做什麼? - JoelFernandes – ishu

相關問題