类似于 Youtube 应用的 Android VideoView 方向

标签 android fullscreen android-videoview screen-orientation onconfigurationchanged

我正在尝试在我的应用中复制 Youtube 应用在播放视频时所做的事情。

纵向模式:仅使用 40% 的屏幕显示视频,其余部分显示有关视频的信息。

横向模式:以全屏模式显示视频和窗口。

我在 stack overflow 和其他论坛中看到了很多关于这个主题的话题,但似乎没有一个适合我。目前我的 videoplayeractivity 有 android:configChanges="keyboardHidden|orientation|screenSize"并且我已经使用以下代码覆盖了 onConfigurationChanged 事件:

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

    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        // requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    }
    else
    {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    }
}

这不会将应用程序置于全屏状态,如果我取消对 requestwindowfeature 的注释,它将崩溃,因为“在添加内容之前必须调用 requestFeature()”。后来我尝试删除 onConfigurationChanged 事件和我尝试过的 onCreate 事件:

@Override
public void onCreate(Bundle savedInstanceState)
{
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_video);

但是,如果我在纵向上开始 Activity 然后翻转到横向,则会在 com.android.internal.policy.impl.PhoneWindow$PanelFeatureState.onRestoreInstanceState(PhoneWindow.java:3527) 处出现 NullPointerException 崩溃。有什么建议吗?

最佳答案

全屏显示:

video_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
    android:id="@+id/video"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />

Activity :

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
   }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  }

并支持像 youtube 应用程序这样的方向更改: Toggling fullscreen & orientation like YouTube

关于类似于 Youtube 应用的 Android VideoView 方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16471564/

相关文章:

windows - 如何制作虚幻引擎 4 全屏(Windows 包)

android - 在 Android 上的 videoview 中播放 m3u8

java - 无法在我的应用中播放 YouTube 视频

java - Admob 问题 - 无法实例化 fragment com.google.android.gms.$PlaceholderFragment

android - 在不发布应用程序的情况下使用 GCM

android - 如何在 Android 的聊天或消息应用程序中发送表情符号(图像、笑脸)?

javascript - 检查是否启用全屏、跨浏览器

android - 有没有办法在本地编译 Air Mobile 应用程序?

android - 如何在 Android 中将 Activity 设置为全屏模式?

android - 缩放 SurfaceView 时视频不缩放