我在应用程序中使用Youtube Player api播放Youtube视频。我想从36秒开始视频,并想在65秒结束视频。
示例:https://www.youtube.com/v/BmOpD46eZoA?start=36&end=65
我正在使用youtubeAndroidPlayerApi。我没有找到Youtubeplayer设置开始值和结束值的任何方法。有人能建议我如何设置参数吗。
我还希望使用youtubeapi中的"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);
}
}
好吧,您可以使用Handler来跟踪视频的当前运行时间。当以毫秒为单位的时间达到您想要停止视频的点时,只需调用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);
}
}
我不确定是否还有其他办法。希望它能解决你的问题。
附言:这里问了一个重复的问题,我在那里更新了我的答案。