有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在使用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. # 1 楼答案

    我为您提供了一个解决方案:

    我有完全相同的问题,在跳进官方提供的例子后,我发现,令我沮丧的是,他们有完全相同的问题。外部控制似乎无法以任何重要方式与玩家互动。更不用说找一个新的YoutubePlayer往往是有问题的。然而,通过测试和尝试不同的东西,我成功地发现了一些东西:

    即使将静态引用保存到YoutubePlayer,此引用也无法以任何方式更改播放器。但您仍然可以检索视频暂停的位置。因此,我下一个合乎逻辑的步骤是尝试:

    1. 离开活动前从玩家处获得位置:

      Integer myMILLIS = yplayer.getCurrentTimeMillis();
      
    2. 然后,回来时重新加载视频,并将其移动到正确的位置:

      // 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. 离开活动前从玩家处获得位置:

      Integer myMILLIS = yplayer.getCurrentTimeMillis();
      
    2. 然后,回来时重新加载视频:

      // Reload the SAME VIDEO again (yplayer is the YoutubePlayer)
      yplayer.cueVideo(mVideoURL);
      
    3. 在做其他事情之前,我们必须等待一点:

      // 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. 再次将其移动到正确的位置,然后播放:

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

    这解决了我的问题。我希望它能解决你的问题