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); } } 提前感谢您的亲切帮助和建议! 解决方法
我有解决方案:
我有完全相同的问题,在跳到官方提供的例子后,我发现,令我沮丧的是,他们有完全相同的问题.外部控制似乎无法以任何重要方式与玩家互动.更不用说获得一个新鲜的YoutubePlayer往往会有问题.然而,我设法通过测试和尝试不同的东西来发现一些东西: 即使您将静态引用保存到YoutubePlayer,此引用也无法以任何方式更改播放器.但您仍然可以检索视频暂停的位置.那么,对我来说,下一个合乎逻辑的步骤就是试试这个: >在离开活动之前从玩家获得位置: Integer myMILLIS = yplayer.getCurrentTimeMillis(); >然后,当重新加载视频时,将其移动到正确的位置: // 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); 在WILL FAIL之后立即调用其他任何东西.因此,这可能是一个奇怪的解决方案,但它确实完美地工作: >在离开活动之前从玩家获得位置: Integer myMILLIS = yplayer.getCurrentTimeMillis(); >然后,当重新加载视频时: // Reload the SAME VIDEO again (yplayer is the YoutubePlayer) yplayer.cueVideo(mVideoURL); >在做任何其他事情之前,我们要等一下: // 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 } >再次将其移动到正确位置,然后播放: // Get the position you saved back yplayer.seekToMillis(myMILLIS); // Start Playing the video from that position again yplayer.play(); 这解决了我的问题.我希望它可以解决你的问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |