2017-02-16 96 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 –