android - 如何停止 youtube 视频或在 android 的 youtube-api 中设置缓冲区限制

标签 android video buffer android-youtube-api

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

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

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

最佳答案

检查此代码

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>

关于android - 如何停止 youtube 视频或在 android 的 youtube-api 中设置缓冲区限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26031394/

相关文章:

Android:用点替换导航栏图标

html - 将视频图像放在 HTML 上

Nginx 只允许我的域访问视频 url

javascript - 何时绑定(bind)缓冲区以允许在 WebGL 中独立拖动多个形状存在问题

java - 最佳 Collection 使用?

android - 如何为 Android 编译 OpenSSL 1.1.1

安卓工作室 : “Execution failed for task ' :app:mergeDebugResources'” if project is created on drive C:

将 sys.stdout 缓冲区设置为零的 Python 标准习惯用法不适用于 Unicode

android - 我想合并 SIP 调用和 GSM 调用并进行 session

image - 使用 FFMPEG 将带有缩放 + 旋转的图像添加到视频叠加层?