有没有办法恢复YouTube API视频?

4
我正在使用YouTube API为Android编写一个简单的应用程序-我希望能够实现选择应用程序中的每个视频/直播流的自动恢复。当我初始化时,我可以设置自动播放。但是,我目前遇到的问题是,当我像下面的代码一样尝试执行时,它并没有按照我想要的方式进行(每次我将该应用程序置于后台并恢复它时,它都会显示播放按钮)。
public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnFullscreenListener, YouTubePlayer.OnInitializedListener {

    //private YouTubePlayer player = null;
    private boolean isFullscreen;
    private static final int RECOVERY_DIALOG_REQUEST = 1;
    private YouTubePlayerView youTubePlayerView;

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

        /** Initializing YouTube Player View for displaying YouTube video **/
        youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
        youTubePlayerView.initialize(DeveloperKey.DEVELOPER_KEY, this);

    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {

        player.setFullscreen(true);
        player.setOnFullscreenListener(this);

        if (!wasRestored) {
            player.loadVideo(YouTubeVideoId.VIDEO_ID);
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                        YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onFullscreen(boolean isFullscreen) {
        this.isFullscreen = isFullscreen;
    }

    @Override
    public void onResume() {
        super.onResume();
        youTubePlayerView.initialize(DeveloperKey.DEVELOPER_KEY, this);
    }

}

非常感谢您的帮助和建议!

1个回答

1
我有解决方案:
我曾经遇到过完全相同的问题,尝试了官方提供的示例后,发现他们也面临着完全相同的问题。外部控件似乎无法以任何重要的方式与播放器进行交互。更不用说获取一个新的YoutubePlayer往往会出现问题。然而,通过测试和尝试不同的方法,我成功地发现了一些东西:
即使你保存了一个静态引用到你的YoutubePlayer,这个引用也无法改变播放器的任何方式。但是你仍然可以检索到视频暂停的位置。因此,对我来说下一个逻辑步骤是尝试这样做:
  1. Getting the position from the player before leaving the activity:

    Integer myMILLIS = yplayer.getCurrentTimeMillis();
    
  2. Then, when coming back reload the video, and move it to the right position:

    // Reload the SAME VIDEO again (yplayer is the YoutubePlayer)
    yplayer.cueVideo(mVideoURL);
    // Get the position you saved back
    yplayer.seekToMillis(myMILLIS);
    // Start Playing the video from that position again
    yplayer.play();
    
事实上,由于某种奇怪的原因,这一方法单独使用并不能起作用。但是当我尝试调试之前的代码时,有时候它竟然能够正常工作。
经过更多的测试后,我发现了之前解决方案的问题:在调用之后...
    yplayer.cueVideo(mVideoURL);

调用其他内容会立即失败。因此,这可能是一个奇怪的解决方案,但它确实完美地工作:
  1. Getting the position from the player before leaving the activity:

    Integer myMILLIS = yplayer.getCurrentTimeMillis();
    
  2. Then, when coming back reload the video:

    // Reload the SAME VIDEO again (yplayer is the YoutubePlayer)
    yplayer.cueVideo(mVideoURL);
    
  3. Before doing anything else, we have to wait a little:

    // The next pause is here to allow the video to load
    try
    {
        Thread.sleep(1000);
    } catch (InterruptedException e)
    {
        // Do something here if you need to
    }
    
  4. Move it to the right position again, and play it:

    // Get the position you saved back
    yplayer.seekToMillis(myMILLIS);
    // Start Playing the video from that position again
    yplayer.play();
    

那解决了我的问题。希望它也解决了你的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接