YouTubePlayer: going fullscreen from portrait phone orientation can easily get out of fullscreen

允我心安 提交于 2019-12-11 04:14:02

问题


If you hold the phone in portrait orientation and you click fullscreen button (of YouTubePlayerView) it easily can get you back (turns off fullscreen) if you shake the phone a little bit

Official YouTube app works normally: you press fullscreen button while holding the phone in portrait orientation and if you shake it a little bit it still stays in fullscreen landscape mode. And if you rotate your phone to landscape orientation and back again to portrait orientation only then it turns off fullscreen (as it should be)

So I would like to implement the same behavior from official YouTube app

I tried to set different Fullscreen flags for YouTubePlayer but it doesn't affect anything

activity manifest

<activity
    android:label="@string/playerview_demo_name"
    android:name=".PlayerViewDemoActivity"
    android:screenOrientation="user"
    android:configChanges="orientation|screenSize|keyboardHidden">
  <intent-filter >
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

activity java

public class PlayerViewDemoActivity extends YouTubeFailureRecoveryActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playerview);
    YouTubePlayerView youTubeView = findViewById(R.id.youtube_view);
    youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this);
  }

  @Override
  public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
                                      boolean wasRestored) {
    if (!wasRestored) {
      player.cueVideo("wKJ9KzGQq0w");
      /*
      FULLSCREEN_FLAG_CONTROL_ORIENTATION;
      FULLSCREEN_FLAG_CONTROL_SYSTEM_UI;
      FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
      FULLSCREEN_FLAG_CUSTOM_LAYOUT;
      */
      player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);

  }

  @Override
  protected YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView) findViewById(R.id.youtube_view);
  }
}

Mb YouTube app does something like this:

after pressing fullscreen button they set (only if programmatic phone display's orientation was in portrait mode)

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);

then they use device sensors to detect approximate orientation (if you hold the phone in portrait/landscape orientation) - Detect Android device orientation (NOT screen orientation)

when you rotate the phone with your hands to landscape orientation they set

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);

来源:https://stackoverflow.com/questions/53242972/youtubeplayer-going-fullscreen-from-portrait-phone-orientation-can-easily-get-o

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