简体   繁体   English

在Android中自动旋转全屏视频

[英]Auto rotate fullscreen video in android

I am a newbie, and I am trying to make a simple application that can play video from url. 我是新手,我正在尝试制作一个可以从url播放视频的简单应用程序。 I am able to play video successfully in my app, but I want it auto rotate,hide actionbar title and fullscreen when I click button fullscreen. 我可以在我的应用中成功播放视频,但是当我单击全屏按钮时,我希望它自动旋转,隐藏动作栏标题和全屏。

This is my manifest : 这是我的清单

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="htantruc.videotest">
    <uses-permission android:name="android.permission.INTERNET" />
       <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
        <activity android:name=".video"
            android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".VideoViewActivity"
            android:configChanges="orientation|screenSize"></activity>
    </application>
</manifest>

and my xml : 和我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".VideoViewActivity"
    android:theme="@style/CustomActivityThemeNoActionBar">
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="250dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">
        <com.github.rtoshiro.view.video.FullscreenVideoLayout
            android:id="@+id/videoview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
    </FrameLayout>
</RelativeLayout>
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

If you're not in an Activity you can get the default Display via WINDOW_SERVICE: 如果您不在活动中,则可以通过WINDOW_SERVICE获取默认的显示:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated: 在引入getSize之前(在API级别13中),可以使用现已弃用的getWidth和getHeight方法:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

How to set width and height 如何设置宽度和高度

videoView.getHolder().setFixedSize(width, height);

Call same code in your activity and onConfigurationChanged() 在您的活动和onConfigurationChanged()调用相同的代码

1-Try this for Auto-Rotate - add these to Manifest : 1-尝试进行自动旋转-将其添加到清单中:

android:configChanges="orientation|screenSize" android:configChanges =“ orientation | screenSize”

android:screenOrientation="portrait" android:screenOrientation =“ portrait”

Please implement your fullscreen button click as below: 请实现您的全屏按钮单击,如下所示:

 public void onFullScreenClick(View v)
 {
     getActionbar().hide();
     getActionbar().show();


     //doesn't work then try
     //getSupportActionBar().hide();
     //getSupportActionBar().show();

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

     // For Landscape above line and For Portrait find Below
     //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
 }

Note: Don't forget to add line 注意:不要忘记添加行

android:configChanges="orientation|screenSize" in manifest file 清单文件中的android:configChanges =“ orientation | screenSize”

Hope this will work 希望这会起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM