Youtube SDK 播放器在切换到横向时无法播放



我的安卓应用程序我实现了youtube SDK,当我点击播放时,它正在播放。当我旋转到横向时,它仅播放纵向模式时间缓冲数据。我只有一个布局

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        setContentView(R.layout.activity_you_tube);
        setTitle("YouTube Link");
        //translucent();
        if (savedInstanceState != null) {
            onRestoreInstanceState(savedInstanceState);
            youtubeURL = savedInstanceState.getString("YouTubeURL");
            updateUI();
        }
        initeUI();
    } catch (Exception e) {
        Log.e(TAG, "onCreate  " + e.getMessage());
    }
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
try {
    this.player = player;
    progress.setVisibility(View.GONE);
    youtubesubmit.setVisibility(View.VISIBLE);
    metadataView.setVisibility(View.VISIBLE);
    updateUI();
    if (!wasRestored) {
        player.cueVideo(youtubeID);
    }
} catch (Exception e) {
    Log.e(TAG, "onInitializationSuccess " + e.getMessage());
}
}

 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    int controlFlags = player.getFullscreenControlFlags();
    if (isChecked) {
        // If you use the FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE, your activity's normal UI
        // should never be laid out in landscape mode (since the video will be fullscreen whenever the
        // activity is in landscape orientation). Therefore you should set the activity's requested
        // orientation to portrait. Typically you would do this in your AndroidManifest.xml, we do it
        // programmatically here since this activity demos fullscreen behavior both with and without
        // this flag).
        setRequestedOrientation(PORTRAIT_ORIENTATION);
        controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        controlFlags &= ~YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
    }
    player.setFullscreenControlFlags(controlFlags);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int controlFlags = player.getFullscreenControlFlags();
    try {
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
            controlFlags &= ~YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
            player.setFullscreenControlFlags(controlFlags);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        }
    } catch (Exception e) {
        Log.e(TAG, "onConfigurationChanged " + e.getMessage());
    }
}

你能帮忙吗

我所做的是

1st. 我去清单.xml并添加 android:config更改到我的播放器活动

 <activity android:name=".Activities.YoutubePlayerActivity"
                android:configChanges="orientation|keyboardHidden|screenSize"></activity>

2nd. 我覆盖配置已更改

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        if (player.getFullscreenControlFlags() == player.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE) {
            player.setFullscreen(true);
            player.play();
        }
    }
}

大功告成。基本上在配置更改上,我检查屏幕是否进入横向模式,youtubePlayer将首先设置为全屏,然后播放。否则,在恢复视频之前,您会有短暂的停顿

相关内容

  • 没有找到相关文章

最新更新