“无法播放此视频”在Android Studio上使用VideoView ...尝试了很多东西但它仍然无效

问题描述 投票:1回答:1

嗨我正在尝试使用帧布局中的相对布局在Android Studio应用程序上播放432x320 .mp4视频(我手动转换为.mp4 H.264格式)。我一直收到“无法播放此视频错误”。

这是我作为.xml文件的内容(注意,我有很多按钮/图像按钮,我在帖子中省略了因为它们无关紧要,占用了大量空间)...

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.kenneth.alphieii.AlphieII">

<ImageView
    android:id="@+id/Robot"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:src="@drawable/alphie_robot1"
    android:scaleType="fitCenter"/>

<TextView android:id="@+id/mytexttester"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:layout_marginTop="19dp"
    android:visibility="gone"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />


...a bunch of buttons here including Yellowb0...this is not an error

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

       <VideoView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/videoView"
        android:layout_width="250dp"
        android:layout_height="150dp"

        android:layout_above="@+id/Yellowb0"
        android:layout_centerHorizontal="true"
        android:visibility="visible" />



</RelativeLayout>

</FrameLayout>

我在这里发布了实际视频,可以轻松下载...

https://streamable.com/y01zy

我有一张当前界面的图片(注意视频大小需要调整)

我尝试过很多东西,比如......

 mVideoView.setVideoPath("android.resource://com.example.s3project/raw/" +
 R.raw.alphie_vidprompt);
 mVideoView.start();

和...

    String uriPath = "android.resource://" +
    "com.android.engineersdream.ed_video/" + R.raw.alphie_vidprompt;
    Uri uri = Uri.parse(uriPath);
    mVideoView.setVideoURI(uri);
    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {

  mVideoView.start();
    }
});

尽管尝试向那些在播放视频视频时遇到类似问题的人提出建议,但我还是无法完成任何工作。还有其他建议我可能会失踪吗?我的视频格式不合适吗?还有另一种在Android Studio上播放视频的简单方法吗?谢谢。

编辑:我尝试按照建议将视频.mp4文件复制到SD卡,它仍然无法正常工作。我已将它们放在Android Manifest.xml文件中:android.permission.READ_EXTERNAL_STORAGE和android.permission.WRITE_EXTERNAL_STORAGE。

这就是我对OnCreate所拥有的......

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alphie_ii);
    mVideoView = (VideoView)findViewById(R.id.videoView);
    push = (ImageButton) findViewById(R.id.On);
    //imgButton.setOnClickListener(imgButtonHandler);
    final int[] mvids = new int[] { R.raw.alphie_vidprompt, R.raw.alphie_vidcorrect };
    for (int i = 0; i < mvids.length; i++) {
        try {
            String path = Environment.getExternalStorageDirectory() + "/Alphie";
            File dir = new File(path);
            if (dir.mkdirs() || dir.isDirectory()) {
                String str_song_name = i + ".mp4";
                CopyRAWtoSDCard(mvids[i], path + File.separator + str_song_name);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private void CopyRAWtoSDCard(int id, String path) throws IOException {
    InputStream in = getResources().openRawResource(id);
    FileOutputStream out = new FileOutputStream(path);
    byte[] buff = new byte[1024];
    int read = 0;
    try {
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
    } finally {
        in.close();
        out.close();
    }
}

以下是我以前称之为视频的内容......

 Uri vidFile = Uri.parse(
 Environment.getExternalStorageDirectory().getAbsolutePath()
 + "alphie/alphie_vidprompt");

    mVideoView.setVideoURI(vidFile);
    mVideoView.start();

我仍然得到相同的消息“无法播放此视频”虽然我还有很多东西需要学习,但我仍然不明白为什么有必要将视频复制到SD卡。我希望从res / raw目录运行它会很容易。

java android video android-videoview
1个回答
1
投票

你给出了一个错误的包名(com.example.s3projectcom.android.engineersdream.ed_video)。您的布局应该是com.example.kenneth.alphieiicom.example.kenneth。但您应该使用上下文中的包名称,例如:

getPackageName();

所以在你的情况下你应该使用

"android.resource://".concat(getPackageName()).concat("/raw/").concat(R.raw.alphie_vidprompt);
© www.soinside.com 2019 - 2024. All rights reserved.