问题
I am using Youtube API for android app and I'm looking for a way to avoid exiting full-screen mode while switching between landscape modes. Meaning, I don't want to exit full-screen mode while switching the phone to portrait mode. I need the video to always be on full-screen.
Is there a way to make it happen?
My current code for full screen is:
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
Intent intent = getIntent();
String Video_Link = intent.getStringExtra("youtube_id");
player.cueVideo(Video_Link);
player.setFullscreen(true);
}
}
Thanks a lot in advance.
回答1:
On an orientation change, the onCreate method is called. You could try adding this line within the Activity in question in your Manifest file to prevent the onCreate call.
android:configChanges="orientation|keyboardHidden|screenSize|layoutDirection"
and it might maintain the fullscreen status of youtube for you...
otherwise you might try overriding the onconfigchange method:
@Override //reconfigure display properties on screen rotation
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
// handle change here
}
else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
// or here
}
}
回答2:
In my situation the activity needed to be in portrait orientation but the video in landscape at all times.
As Will Thomson mentioned above you'll want to add this to your manifest:
android:configChanges="orientation|keyboardHidden|screenSize"
This may not be the ideal way to do things but does what I need.
You can set a fullscreen listener after initialization:
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
this.player = player;
player.setOnFullscreenListener(this);
if(!wasRestored) {
player.cueVideo(Constants.INTO_YOUTUBE_VIDEO_ID);
}
}
Then in the callback you can manually set the orientation:
@Override
public void onFullscreen(boolean fullscreen) {
videoIsFullScreen = fullscreen;
if(fullscreen) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
When the user taps play you can use player.setFullscreen(true)
. When the user taps the back button you can exit fullscreen or finish the activity.
@Override
public void onBackPressed() {
if(player != null && videoIsFullScreen) {
player.setFullscreen(false);
}
else {
super.onBackPressed();
}
}
There's another similar answer here https://stackoverflow.com/a/39494759/1159930 but following the first comment played the video in fullscreen portrait mode rather than landscape.
来源:https://stackoverflow.com/questions/25432397/full-screen-goes-out-when-switching-to-portrait