Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android YouTube api v3 - orientation

Getting a sample activity up and running using YouTubeBaseActivity, YouTubePlayerView and YouTubePlayer was simple enough. I'm having trouble with orientation though and can't find any documentation or sample code. When I switch while a video is playing the screen is blank.

What are the correct things to do in each of onCreate(), onPause(), onSaveInstanceState() and onRestoreInstanceState() to have the video continue playing?

Thanks

like image 899
milleph Avatar asked Feb 21 '13 21:02

milleph


2 Answers

Variables

@SuppressLint("InlinedApi")
private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
        ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;

@SuppressLint("InlinedApi")
private static final int LANDSCAPE_ORIENTATION = Build.VERSION.SDK_INT < 9
        ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        : ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;

private YouTubePlayer mPlayer = null;
private boolean mAutoRotation = false;

OnCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAutoRotation = Settings.System.getInt(getContentResolver(),
            Settings.System.ACCELEROMETER_ROTATION, 0) == 1;
}

Implement OnInitializedListener

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
        boolean wasRestored) {
    mPlayer = player;
    player.setOnFullscreenListener(this);
    
    if (mAutoRotation) {
        player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
                | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
                | YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE
                | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
    } else {
        player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
                | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
                | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
    }
}

Inplement onConfigurationChanged

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        if (mPlayer != null)
            mPlayer.setFullscreen(true);
    } 
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        if (mPlayer != null)
            mPlayer.setFullscreen(false);
    }
}

@Override
public void onFullscreen(boolean fullsize) {
    if (fullsize) {
        setRequestedOrientation(LANDSCAPE_ORIENTATION);
    } else {
        setRequestedOrientation(PORTRAIT_ORIENTATION);
    }
}

Menifest

 <activity
    android:name="com.sample.android.YouTubePlayerActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:screenOrientation="sensor"
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>

I've made sample activity which uses most recent youtube api.

This source handle "Orientation Problem", "Media Volume Problem", "Youtube Url Parsing Problem"

  1. Here is git project for sample app

    https://github.com/TheFinestArtist/YouTubePlayerActivity

  2. I also made sample app you can download

    https://play.google.com/store/apps/details?id=com.thefinestartist.ytpa.sample

like image 69
The Finest Artist Avatar answered Sep 27 '22 20:09

The Finest Artist


Leaving this here for others. The Finest Artist's answer worked for me as well, but since I'm using a video player inside a fragment and have to support the AppCompatLibrary as well, here's what I was able to get working:

YouTubePlayerActivityFragment.java

public class YouTubePlayerActivityFragment extends YouTubePlayerSupportFragment implements
    YouTubePlayer.OnFullscreenListener {

@SuppressLint("InlinedApi")
private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
        ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;

@SuppressLint("InlinedApi")
private static final int LANDSCAPE_ORIENTATION = Build.VERSION.SDK_INT < 9
        ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        : ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;

private YouTubePlayer.OnFullscreenListener fullScreenListener = null;
private YouTubePlayer yPlayer = null;
private boolean mAutoRotation = false;


public static YouTubePlayerActivityFragment newInstance(String videoID, String apiKey) {
    YouTubePlayerActivityFragment fragment = new YouTubePlayerActivityFragment();
    Bundle bundle = new Bundle();
    bundle.putString("video_id", videoID);
    bundle.putString("api_key", apiKey);
    fragment.setArguments(bundle);
    fragment.init();
    return fragment;
}

public YouTubePlayerActivityFragment() {

}

@Override
public void onCreate(Bundle var1) {
    super.onCreate(var1);
    mAutoRotation = Settings.System.getInt(getActivity().getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1;
    fullScreenListener = this;
}

private void init() {
    initialize(getArguments().getString("api_key"), new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
            yPlayer = youTubePlayer;
            youTubePlayer.setOnFullscreenListener(fullScreenListener);
            if (mAutoRotation) {
                youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
                        | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
                        | YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE
                        | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
            } else {
                youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
                        | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
                        | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
            }

            if (!wasRestored) {
                yPlayer.loadVideo(getArguments().getString("video_id"));
            }
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        }
    });
}

@Override
public void onFullscreen(boolean isFullSize) {
    if (isFullSize) {
        getActivity().setRequestedOrientation(LANDSCAPE_ORIENTATION);
    } else {
        getActivity().setRequestedOrientation(PORTRAIT_ORIENTATION);
    }
}
like image 21
akousmata Avatar answered Sep 27 '22 19:09

akousmata