在Android应用程序中播放项目内视频



我正在尝试创建一个应用程序,其中我的可绘制mdpi文件夹中有一个视频(d1)。我希望在用户启动应用程序时播放此视频。

这是我第一次尝试这样的东西,所以不知道该怎么做。

My MainActivity.Java OnCreate方法代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    VideoView vv = (VideoView)this.findViewById(R.id.vv01);
    String uriString = "android.resource://" + getPackageName() + "/" + R.drawable.d1;
    vv.setVideoURI(Uri.parse(uriString));
    vv.start();
}

我的Activity_Main.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" >
    <VideoView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/vv01"
        android:contentDescription="@string/app_name"
        />
</RelativeLayout>

我不确定我使用的URI字符串路径是否正确。请建议如何实现它,因为它目前不起作用。此外,如果可能的话,请建议如何包括播放/暂停按钮和重新定向到新视频的"下一个视频"按钮。

您可以将视频文件放在assets文件夹中,然后像这样播放

vv.setVideoPath("file:///android_asset/d1.avi");    
vv.start();

最新更新