我正在使用 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 的静态引用,此引用也无法以任何方式更改播放器。但是您仍然可以检索视频暂停的位置。所以,对我来说,下一个合乎逻辑的步骤是尝试这个:
在离开 Activity 之前从玩家处获取位置:
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();
问题是,由于某些奇怪的原因,单独这样做是行不通的,但是当我尝试调试之前的代码 fragment 时……它有时会正常工作。
所以,经过更多的测试,我发现了之前解决方案的问题:调用之后
yplayer.cueVideo(mVideoURL);
在将失败之后立即调用任何其他内容。因此,这可能是一个奇怪的解决方案,但它确实可以完美地工作:
在离开 Activity 之前从玩家处获取位置:
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();
这解决了我的问题。我希望它能解决你的问题。
关于java - 有什么方法可以恢复 YouTube API 视频吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34555305/