问题
I have a YouTube API fragment that is statically added in my xml manifest file, i.e. a fragment that has a youtube player inside of it.
I do not have a file that extends fragment in my project.
In my activity class I put this line of code in the onCreate
of my activity class:
youTubePlayerFragment.setRetainInstance(true);
It does not have any affect: when I rotate the screen I get a blank black screen of the fragment.
How can I get it to continue playing the fragment after the orientation change?
<fragment
android:id="@+id/youtube_fragment"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_width="900dp"
android:layout_height="500dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
回答1:
The quick and easy solution is to add the following to your video activity, in your manifest:
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
I have an app that uses this for a video and it works just fine. Everything lays out properly on orientation change, and the activity and fragment instance do not get torn down allowing it to seamlessly continue to play.
If you want the activity to be torn down/recreated, while retaining the fragment instance, please re-read the documentation for setRetainInstance()
. There are some subtle nuances that you need to be aware of to get this to work properly: http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)
回答2:
Every time orientation changes the activity will be recreated as long as with all child components. Now the important part is that, YouTubePlayer.Provider will hold its stages(such as:loaded videos, the current playback position and player configurations). Have a look at the following:
YouTubePlayer Overview
In your case, after the orientation changes, the activity is recreated as long as its child fragment(youTubePlayerFragment). So you lost the reference of the YouTubePlayer's instant and the data(such as videoID or video url) which are necessary to load the video, But the YouTubePlayer's provider is still holding the previous state, which become null after the rotation.
Solution: you actually need to manage a way to save the data necessary for playing the video on YouTubePlayer before the device change the orientation, and to retrieve the data back when the activity is recreated. Have a look at the following:
YouTubePlayerFragment Overview
Not sure about your code structure, but hope the following code will give you some idea:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("currentVideoID",videoID);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
videoID = savedInstanceState.getString("currentVideoID");
}
A sample to get the video playing on youtubeplayer
private void loadYouTubePlayer(){
//load your youTubePlayerFragment here, i used YouTubePlayerSupportFragment(),may change in your case
//also you may not need to call getActivity(). Change the code as require
youTubePlayerFragment = (YouTubePlayerSupportFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(developerKey,new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
myYouTubePlayer = youTubePlayer;
myYouTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION | YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
myYouTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
@Override
public void onFullscreen(boolean b) {
isFullScreenPlaying = b;
Log.d(null,"Now fullScreen");
}
});
if (!b) {
myYouTubePlayer.loadVideo(videoID);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.e(null,"Initialization Failed !!!");
}
});
}
来源:https://stackoverflow.com/questions/15871050/how-to-retain-instance-of-fragment-of-fragment-playing-video-after-change-in-ori