当在设备上创建活动时,Youtube播放器已被释放

时间:2014-11-14 12:20:12

标签: android youtube android-studio youtube-api

当设备旋转并重新创建活动时,YouTube播放器已发布。然后播放视频。显示YouTube播放器已发布。我不知道它在哪里发布。我已经将它设置为Null onDestroy Method。

`mPlayer.loadVideo(videoId);`
  

//指向YouTube播放器发布

3 个答案:

答案 0 :(得分:2)

我的解决方案是在触发屏幕轮换时,getCurrentTimeMillis()中的YouTubePlayer onSaveInstanceState(Bundle state)保存当前播放时间,并从onRestoreInstanceState(Bundle state)获取时间。

示例代码为:

static final String CURRENT_PLAY_TIME = "current_play_time";
int current_time;

@Override
protected void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);
    state.putInt(CURRENT_PLAY_TIME, youTubePlayer.getCurrentTimeMillis());
}

@Override
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
    current_time = state.getInt(CURRENT_PLAY_TIME);
}

恢复当前的播放时间后,让YouTubePlayer加载视频。

youTubePlayer.loadVideo(VIDEO_ID, current_time);

希望它有所帮助!

答案 1 :(得分:0)

一种可能的解决方案是让您的应用垂直或水平永久

为了在你的清单中写这个:

<android:screenOrientation="portrait"> //vertical
<android:screenOrientation="landscape"> //horizontal

答案 2 :(得分:0)

I have found my Excelent solution..all going good with youtube player
@Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);

            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                if (mPlayer != null)
                    //Set YouTube Player here;
            }
            if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                if (mPlayer != null)
                  //Set YouTube Player here;
            }
        }