android 用于背景的视频无法播放

xe55xuns  于 2023-03-11  发布在  Android
关注(0)|答案(1)|浏览(129)

我想在我的应用程序的登录/注册页面上放一段背景视频
我制作了videobg.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/bgVideoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:keepScreenOn="true" />

</RelativeLayout>

之后我创建了videologreg.java

public class videologreg extends AppCompatActivity {
    private VideoView mVideoView;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //to play a video on the login background
        mVideoView = (VideoView) findViewById(R.id.bgVideoView);


        Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.asf);
        mVideoView.setVideoURI(uri);
        mVideoView.start();

        mVideoView.setOnPreparedListener(mediaPlayer -> mediaPlayer.setLooping(true));



    }
}

最后我进入了注册登录布局

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include layout="@layout/videopozadina" />

然而,视频根本不播放,所以我希望你的帮助,提前感谢。
我已经尝试了以上所有的事情,但不幸的是,它不工作
关于黑屏我得到了什么

zujrkrfu

zujrkrfu1#

所以从提供的屏幕截图来看,我猜它应该来自这个xml:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include layout="@layout/videopozadina" />
    <!-- There should be more layout design here -->

并且您需要确保对于这个布局videopozadina,它已经在那里定义了VideoView,我相信它应该是您提供的如下内容:

<?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/bgVideoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:keepScreenOn="true" />

</RelativeLayout>

然后,您需要在这里提到的第一个xml中为包含的布局进一步定义一个ID:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include id="@+id/videoLayout" 
             layout="@layout/videopozadina" />
   <!-- There should be more layout design here -->

并且您应该在登录/注册页面的Fragment或Activity中设置视频(我不确定是否为videologreg):

public class videologreg extends AppCompatActivity {
    private VideoView mVideoView;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // I guess you should have setContentView() elsewhere, and the View should be the first mentioned xml, I believe
        // to play a video on the login background
        // You should first retrieve the included layout with the ID first, then further access the VideoView inside the included layout
        mVideoView = (VideoView) findViewById(R.id.videoLayout).findViewById(R.id.bgVideoView);

        Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.asf);
        mVideoView.setVideoURI(uri);
        mVideoView.start();
        mVideoView.setOnPreparedListener(mediaPlayer -> mediaPlayer.setLooping(true));
    }
}

相关问题