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

时间:2016-02-17 10:18:03

标签: android youtube youtube-api

我的Android应用程序我实现了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());
    }
}

你可以请任何帮助

1 个答案:

答案 0 :(得分:0)

我做的是

第一。我去manifest.xml并将android:configChanges添加到我的playerActivity

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

第二。我重写configurationChanged

@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();
        }
    }
}

你已经完成了。基本上在configurationChange上我检查屏幕是否进入横向模式并且youtubePlayer将首先设置为全屏,然后播放。否则你会在恢复视频之前暂停一下