首页 > 解决方案 > Android:无法使用 videoview 播放视频

问题描述

请在下面找到xml

<VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />

Java代码:

  String url = "android.resource://com.example.androidtutorial/" + R.raw.instructions;
  videoView.setVideoURI(Uri.parse(url));
  videoView.start();

Logcat 中显示错误:

2019-10-20 10:51:16.708 5063-5063/? E/androidtutoria: Unknown bits set in runtime_flags: 0x8000
2019-10-20 10:51:22.825 5063-5080/com.example.androidtutorial E/MediaPlayerNative: error (1, -2147483648)
2019-10-20 10:51:23.178 5063-5063/com.example.androidtutorial E/MediaPlayer: Error (1,-2147483648)

我在互联网上看到了许多使用相同代码的示例,但就我而言,它不起作用。

标签: androidandroid-studioandroid-layoutandroid-videoview

解决方案


我建议您使用 WebView 而不是视频视图。

这会更好。

但是为了你的答案

1.检查你的连接

2.检查您的权限

3.检查您使用 Debug 构建的 Url

4.将start方法放入try catch

现在它在这一行似乎有问题:

videoView.setVideoURI(Uri.parse(url));

uri 表示您设备中的文件路径而不是 url,请检查并重试。

你可以在评论中更新我

编辑:使用此方法:

public Uri getRawUri(String filename) {
    return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + 
File.pathSeparator + File.separator + getPackageName() + "/raw/" + 
filename);
}

下一个解决方案:使用Uri.fromFile代替Uri.parse

使用这两种方法:

video.setVideoURI(URI);
video.setMediaController(new MediaController(this));
video.requestFocus();
video.start();

onCreate()在你的方法中试试这个:

Uri URI = getRawUri(instructions);
MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(video)

video.setVideoURI(URI);
video.setMediaController(mediaController);
video.requestFocus();
video.start();

推荐阅读