如何使VideoView全屏

时间:2016-01-21 08:48:34

标签: android android-videoview landscape android-fullscreen

我希望使用Activity在我的VideoView中播放视频,并在点击{{landscape时将其设为全屏和Button模式(隐藏虚拟按钮和状态栏) 1}}。

但它无法隐藏虚拟按钮,底部有一条白线。 my app

  

这是我的活动代码:

public class VideoActivity extends Activity {
private VideoView mVideoView;
private String mUrl;
private Button mFullScreen;
private static String TAG = VideoActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG,"onCreate");
    setContentView(R.layout.video);
    mVideoView = (VideoView) findViewById(R.id.video);
    mFullScreen = (Button) findViewById(R.id.fullscreen);
    File file = new File(Environment.getExternalStorageDirectory(),"video.mp4");
    mVideoView.setVideoPath(file.getPath());
    mVideoView.start();
    mFullScreen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            enterFullScreen();
            mFullScreen.setVisibility(View.GONE);
        }
    });

}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG,"onDestroy");
}

private void enterFullScreen(){
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//常亮
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.MATCH_PARENT,
      RelativeLayout.LayoutParams.MATCH_PARENT
    );
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  }
}

video.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <VideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp"/>
    <Button
        android:id="@+id/fullscreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:text="Fullscreen"/>
</RelativeLayout>

4 个答案:

答案 0 :(得分:3)

在横向模式下尝试此操作。

<VideoView android:id="@+id/video"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
   android:layout_alignParentRight="true"
   android:layout_alignParentBottom="true"
   android:layout_alignParentTop="true"
    />

隐藏虚拟按钮添加此代码:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

原始尺寸

         DisplayMetrics metrics = new DisplayMetrics();  getWindowManager().getDefaultDisplay().getMetrics(metrics);
         android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
         params.width =  (int) (300*metrics.density);
         params.height = (int) (250*metrics.density);
         params.leftMargin = 30;
         videoView.setLayoutParams(params);

全屏尺寸

         DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
         android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
         params.width =  metrics.widthPixels;
         params.height = metrics.heightPixels;
         params.leftMargin = 0;
         videoView.setLayoutParams(params);

答案 1 :(得分:1)

最后,我用Kristo1990和prashantwosti的帮助解决了这个问题 我的代码是:

const authTransition = function authTransition(nextState, replace, callback) {
  const state = store.getState()
  const user = state.user

  // todo: in react-router 2.0, you can pass a single object to replace :)
  if (!user.isAuthenticated) {
    replace({ nextPathname: nextState.location.pathname }, '/login', nextState.location.query)
  }

  callback()
}

最后我重写了KEYCODE_BACK以退出全屏 我希望它可以帮助你们所有人,再次感谢你们。

答案 2 :(得分:0)

您可以将其用于BackButton

closeButton = (Button) findViewById(R.id.buttonClose);
    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Log.d("VideoPreview", "onClick Close Button");
            VideoFullscreenActivity.super.onBackPressed();
        }
    });

看看:How to close a VideoView Activity (currently have to press back twice)

答案 3 :(得分:0)

以下是我工作应用中的一些片段:

玩家活动中的

decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

你的player_layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/video_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:keepScreenOn="true"
android:orientation="vertical">

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".PlayerActivity">

   <VideoView
     android:id="@+id/videoView"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_gravity="center"/>

</FrameLayout>


</RelativeLayout>

值/ styles.xml:

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>