How to provide start and end parameters for a youtube video in youtube Android player?(如何在 youtube Android 播放器中为 youtube 视频提供开始和结束参数?)
问题描述
我正在使用 Youtube Player api 在我的应用程序中播放 youtube 视频.我想从假设 36 秒开始视频,并希望在 65 秒左右结束视频.
I am using an Youtube Player api for playing youtube videos in my application. I want to start the video from suppose 36sec and wants to end this video at some 65sec.
示例:https://www.youtube.com/v/BmOpD46eZoA?start=36&end=65
我正在使用 youtubeAndroidPlayer Api.我没有找到任何 Youtubeplayer 设置开始和结束值的方法.有人可以建议我如何设置参数.
I am using youtubeAndroidPlayer Api. I didnt find any methods for Youtubeplayer to set the start and end values.could anyone suggest how can I set the parameters.
而且我还希望使用 youtube api 中的控件"字段隐藏控件,但我没有找到任何方法来隐藏它.
And I also want the controls to be hide using the "controls" field in youtube api,but I didnt find any methods to hide this.
我的 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()
方法.
Well, you can make use of Handler to track the current running time of the video. When the time in milliseconds has reached the point where you want to stop the video, simply call the player.pause()
method.
这是活动的完整代码:
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);
}
}
我不确定是否还有其他方法.希望它能解决你的问题.
I'm not sure if there's any other way. Hope it solved your problem.
附:这里 并且我在那里更新了我的答案.
P.S. A duplicate question was asked here and I've updated my answer there.
这篇关于如何在 youtube Android 播放器中为 youtube 视频提供开始和结束参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 youtube Android 播放器中为 youtube 视频提供开始和结束参数?
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01