YouTubePlayerFragment black screen on orientation changes

放肆的年华 提交于 2019-12-12 18:45:58

问题


I'm trying to keep playing YouTubePlayerFragment on orientation changes.

the code looks like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
    FrameLayout frameLayout = new FrameLayout(getActivity());
    populateViewForOrientation(inflater, frameLayout);
    return frameLayout;
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    populateViewForOrientation(inflater, (ViewGroup) getView());
}

private void populateViewForOrientation(LayoutInflater inflater, ViewGroup viewGroup) {
    viewGroup.removeAllViewsInLayout();
    rootView = inflater.inflate(getLayoutResId(), viewGroup);
    ButterKnife.bind(this, rootView);

    if (frYTube != null) {
       FragmentTransaction transaction = getFragmentManager().beginTransaction();
       transaction.replace(R.id.frame, frYTube);
       transaction.commitAllowingStateLoss();
    }
}

fragment replaced (i see text on it) and the music is playing but instead of video - black screen. How can i deal with it? is it possible to get video back? btw, if after rotation next video is oppenes - the video appears. i've tried to call pause and play for current video - it dosen't help. i haven't try YouTubePlayerView because i use moxy so i need to deal with fragment.

upd

it looks like to create custom view with YouTubePlayerFragment inside have more stable behavior (and remove add view then). but the black screen problem still exists.

it's possible to restore video in this case with:

 mYouTubePlayer.loadVideo(url);
 ...
 public void onLoaded(String s) {
  mYouTubePlayer.seekToMillis(mills);

but there is no sense too reload video, i'm trying to avoid this - the delay appears


回答1:


Based from this post, make sure that you are initializing the YouTubePlayerView in onCreate() when onCreate() was called during a restore rather than an explicit create. Here's a tutorial in which the activity containing the fragment does not need to extend any special base class.

You may also check this related thread: Screen orientation not working with youtubePlayerfragment




回答2:


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: https://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

Using this it will continue playing the fragment after the orientation change




回答3:


Only way i found and it works is to handle screen orientation manually:

1) add to your activity:

android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

2) in activity class:

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

and you shouldn't inflate new layout. just remove and recreate/add all you need except youtubeframe to make land layout looks you want. it's a bit tricky and strange, but in other cases you got black screen instead video.



来源:https://stackoverflow.com/questions/45526975/youtubeplayerfragment-black-screen-on-orientation-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!