如何在我的Android Webview中启用全屏视频播放

问题描述

我尝试启用图形加速,但对我没有用 这是我的代码 Click here to see 我也收到此错误click here too see

请帮助我,我被卡住了 有人可以帮助我提供代码

解决方法

为此,您可能应该使用VideoView。它允许全屏查看某些网址中的视频。

首先,使用VideoView创建玩家活动xml文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <VideoView android:id="@+id/video_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

在您的AndroidManifest.xml的{​​{1}}中,将PlayerActivity属性设置为screenOrientation

landscape

最后,在您的<activity android:name=".PlayerActivity" android:screenOrientation="landscape" /> 中设置全屏标志并加载视频网址(或本地文件-选中documentation):

PlayerActivity.kt

或者,如果您使用Java编写,请在override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Fullscreen mode requestWindowFeature(Window.FEATURE_NO_TITLE) window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN) setContentView(R.layout.activity_player) // Load your video file video_view.setVideoURI(Uri.parse(intent.getStringExtra(URL_TO_VIEW))) video_view.setMediaController(MediaController(this)) video_view.requestFocus(0) video_view.start() } 中:

PlayerActivity.java