【问题标题】:How to stop youtube video or set buffer limit in youtube-api in android如何在 android 的 youtube-api 中停止 youtube 视频或设置缓冲区限制
【发布时间】:2014-09-25 05:41:57
【问题描述】:

我正在使用 YouTube API 播放我的视频,YouTube 播放器中有 play()pause() 函数。我需要在特定时间停止视频。我正在使用pause(),但视频的背景正在缓冲。如何在特定时间限制后停止缓冲视频?在 webview 中,URL 中有一个选项开始和结束标记来缓冲,如下所示,

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

注意:我使用 Youtube Player 播放视频而不是 WEBVIEW

【问题讨论】:

    标签: android video buffer android-youtube-api


    【解决方案1】:

    检查此代码

    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>
    

    【讨论】:

      猜你喜欢
      • 2013-03-07
      • 2011-10-04
      • 2013-02-03
      • 2016-02-19
      • 2012-07-02
      • 2014-03-14
      • 2020-11-17
      • 2011-11-18
      • 1970-01-01
      相关资源
      最近更新 更多