VideoView:无法打开内容,setDataSource失败

时间:2015-10-13 13:46:00

标签: mp4 android-videoview android-video-player

我无法使用我的应用程序打开任何视频文件。我从StackOverFlow获得了一段代码并使用它,但它对我不起作用。 我有链接,因为它在我的画廊。但它抛出了无法打开内容的运行时错误。但是我可以和其他玩家一起玩,这是用手机拍摄的视频......

public class VideoActivity extends Activity {

private VideoView mVideoView;
String path = "/storage/emulated/0/DCIM/Camera/VID_20151013_150537120.mp4";
//https://www.youtube.com/watch?v=r-M9Xhk026A

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);
    mVideoView = (VideoView) findViewById(R.id.surface_view);
    mVideoView.setVideoPath(path);
    mVideoView.setMediaController(new MediaController(this));
    mVideoView.requestFocus();
    mVideoView.start();
    //Bundle bundle = getIntent().getExtras();
    //String url = bundle.getString("url", "");
    //playVideoFromUrl(url);
}

private void playVideoFromUrl(String url) {
     mVideoView.setVideoURI(Uri.parse(url));
     mVideoView.setMediaController(new MediaController(this));
     mVideoView.requestFocus();
     mVideoView.start();
}  }

错误日志是:

  I/OpenGLRenderer: Initialized EGL, version 1.4
  D/OpenGLRenderer: Enabling debug mode 0
      W/art: Suspending all threads took: 13.357ms
      W/VideoView: Unable to open content: /storage/emulated/0/DCIM/Camera/VID_20151013_150537120.mp4
      W/VideoView: java.io.IOException: setDataSource failed.
      W/VideoView:     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1076)
      W/VideoView:     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1022)
      W/VideoView:     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:974)
      W/VideoView:     at android.widget.VideoView.openVideo(VideoView.java:352)
      W/VideoView:     at android.widget.VideoView.access$2100(VideoView.java:72)
      W/VideoView:     at android.widget.VideoView$7.surfaceCreated(VideoView.java:629)
      W/VideoView:     at android.view.SurfaceView.updateWindow(SurfaceView.java:579)
      W/VideoView:     at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:176)
      W/VideoView:     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
      W/VideoView:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1986)
      W/VideoView:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
      W/VideoView:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5884)
      W/VideoView:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
      W/VideoView:     at android.view.Choreographer.doCallbacks(Choreographer.java:580)
      W/VideoView:     at android.view.Choreographer.doFrame(Choreographer.java:550)
      W/VideoView:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
      W/VideoView:     at android.os.Handler.handleCallback(Handler.java:739)
      W/VideoView:     at android.os.Handler.dispatchMessage(Handler.java:95)
      W/VideoView:     at android.os.Looper.loop(Looper.java:135)
      W/VideoView:     at android.app.ActivityThread.main(ActivityThread.java:5312)
      W/VideoView:     at java.lang.reflect.Method.invoke(Native Method)
      W/VideoView:     at java.lang.reflect.Method.invoke(Method.java:372)
      W/VideoView:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
      W/VideoView:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
      D/VideoView: Error: 1,0

3 个答案:

答案 0 :(得分:1)

我尝试了WRITE_EXTERNAL_STORAGE和READ_EXTERNAL_STORAGE的权限,但没有用。

就我而言,足够放置许可证

<uses-permission android: name = "android.permission.INTERNET" />

答案 1 :(得分:0)

检查AndroidManifest.xml中的Permissions

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"

这可能有所帮助。

答案 2 :(得分:0)

您只需要

但是请注意,这也是RuntimePermission

if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
            return false;
}