2017-02-16 94 views
0

我已经将示例mp4视频保存在android内部存储中的一个文件中,但是当尝试从文件中读取并在android videoview中通过解析文件路径作为uri进行播放时, t播放此视频错误。我检查了很多链接,但没有用,我如何实现这一点。?Android - 保存视频内部存储和播放

final String mFileName = "sample.mp4"; 
try{ 
     FileOutputStream mFileOutputStream = openFileOutput(mFileName, Context.MODE_PRIVATE); 
     mFileOutputStream.write(getResources().getAssets().open("sample_video.mp4").read()); 
     mFileOutputStream.close(); 
     Log.v(TAG, "Success"); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    MediaController mediaController = new MediaController(this); 
    mediaController.setAnchorView(mVideoView); 
    mVideoView.setMediaController(mediaController); 
    mVideoView.setVideoPath(getFilesDir().getAbsolutePath()+"/sample.mp4"); 
    mVideoView.start(); 

回答

0

只是你不能。 VideoView内部使用MediaPlayer,其明确requires a file to be world-readable(应用程序的dirs不是)。正如文档所述,绕过这个限制的唯一方法是传递一个文件描述符而不是文件路径。

可惜VideoView没有一个方法像setDataSource(FileDescriptor),所以你有两种方式:

  1. 访问VideoViewMediaPlayerthrough reflection并调用它setDataSource(FileDescriptor)。不确定这是否是一种可行的解决方案,因为当您拨打VideoView.setVideoPath时,内部媒体播放器会被实例化,但您不应该称之为......简而言之,请评估它,但它可能是死路一条。
  2. 使用MediaPlayer,set a surface holder播放视频并将FileDescriptor直接传递给MediaPlayer(imo此第二种解决方案更清洁)。
+0

是否有可能通过使用内容提供商..? –

+0

应该可以通过传递内容URI('VideoView.setVideoURI()')。只要不传递'file://'这样的uri,如果是这样,内部的'MediaPlayer'将简单地调用'setDataSource(String filepath)'。 – Massimo

+0

能否详细说明一下,我如何在ContentProvider中放置文件路径,以及如何将uri传递给videoview –