3

我正在使用android手機在延遲容忍網絡上進行視頻流式傳輸。對於即時通訊試圖基於由口袋西遊使用android媒體播放器的漸進式視頻下載

http://blog.infidian.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/

在創建一個新的媒體播放器的功能prepareAsync阻止視頻顯示,使得流不堪給出的教程來實現漸進式視頻下載。在媒體播放器準備好的時候是否有辦法顯示某些內容?

我正在使用SurfaceView和SUrfaceHolder來顯示API媒體演示中建議的視頻。

private MediaPlayer createMediaPlayer(File mediaFile) 
throws IOException { 
    MediaPlayer mPlayer = new MediaPlayer(); 
    mPlayer.setOnErrorListener(
      new MediaPlayer.OnErrorListener() { 
       public boolean onError(MediaPlayer mp, int what, int extra) { 
        Log.e(TAG, "Error in MediaPlayer: (" + what +") with extra (" +extra +")"); 
        return true; 
       } 
      }); 

    // It appears that for security/permission reasons, it is better to pass a FileDescriptor rather than a direct path to the File. 
    // Also I have seen errors such as "PVMFErrNotSupported" and "Prepare failed.: status=0x1" if a file path String is passed to 
    // setDataSource(). So unless otherwise noted, we use a FileDescriptor here. 
    FileInputStream fis = new FileInputStream(mediaFile); 
    mPlayer.setDataSource(fis.getFD()); 
    mPlayer.setDisplay(holder); 
    mPlayer.prepareAsync(); 
    return mPlayer; 
} 

/** 
* Transfer buffered data to the MediaPlayer. 
* NOTE: Interacting with a MediaPlayer on a non-main UI thread can cause thread-lock and crashes so 
* this method should always be called using a Handler. 
*/ 
private void transferBufferToMediaPlayer() { 
    try { 
     // First determine if we need to restart the player after transferring data...e.g. perhaps the user pressed pause 
     final boolean wasPlaying = mediaPlayer.isPlaying(); 
     final int curPosition = mediaPlayer.getCurrentPosition(); 
     Log.e(TAG, "Current position: "+curPosition); 
     final MediaPlayer temp = mediaPlayer; 

     // Copy the currently downloaded content to a new buffered File. Store the old File for deleting later. 
     File oldBufferedFile = new File(context.getCacheDir(),"playingMedia" + counter + ".dat"); 
     File bufferedFile = new File(context.getCacheDir(),"playingMedia" + (counter++) + ".dat"); 

     // This may be the last buffered File so ask that it be delete on exit. If it's already deleted, then this won't mean anything. If you want to 
     // keep and track fully downloaded files for later use, write caching code and please send me a copy. 
     bufferedFile.deleteOnExit(); 
     moveFile(downloadingMediaFile,bufferedFile); 

     // Pause the current player now as we are about to create and start a new one. So far (Android v1.5), 
     // this always happens so quickly that the user never realized we've stopped the player and started a new one 
     if(wasPlaying){ 
      Log.e(TAG, "mediaPlayer paused"); 
      mediaPlayer.pause(); 
     } 
     //mediaPlayer.release(); 

     // Create a new MediaPlayer rather than try to re-prepare the prior one. 
     mediaPlayer = createMediaPlayer(bufferedFile); 

     mediaPlayer.setOnPreparedListener(
       new MediaPlayer.OnPreparedListener() { 

        @Override 
        public void onPrepared(MediaPlayer mPlayer) { 
         // TODO Auto-generated method stub 
         mediaPlayer.seekTo(curPosition); 

         // Restart if at end of prior buffered content or mediaPlayer was previously playing. 
         // NOTE: We test for < 1second of data because the media player can stop when there is still 
         // a few milliseconds of data left to play 
         int mediaLength = mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition(); 
         boolean atEndOfFile = mediaLength >= 1000; 
         Log.e(TAG, "MediaLen:"+mediaLength); 
         if (wasPlaying || atEndOfFile){ 
          Log.e(TAG, "starting new player"); 
          temp.release(); 
          mediaPlayer.start(); 
         } 
        } 
       }); 


     // Lastly delete the previously playing buffered File as it's no longer needed. 
     oldBufferedFile.delete(); 

    }catch (Exception e) { 
     Log.e(TAG, "Error updating to newly loaded content.", e);     
    } 
} 

回答

0

考慮使用exoplayer

http://developer.android.com/guide/topics/media/exoplayer.html

它與流內置記從文檔:

ExoPlayer支持當前未被MediaPlayer的提供的功能,其中包括動態自適應流HTTP(DASH),SmoothStreaming和持久緩存。 ExoPlayer可以擴展爲處理其他媒體格式,並且由於您將其作爲應用程序代碼的一部分包含在內,因此您可以將其與應用程序一起更新。

相關問題