2012-07-11 73 views
2

我一直在從我的android應用程序的視頻播放的漸進視頻。我能夠從互聯網上流式傳輸視頻。異步視頻流Android

但緩衝整個數據後,視頻開始。

這裏是我using-

private static final String MOVIEURL = "movieUrl"; 
private static final String TAG = "VideoPlayerActivity"; 
private int mVideoWidth; 
private int mVideoHeight; 
private MediaPlayer mMediaPlayer; 
private SurfaceView mPreview; 
private SurfaceHolder holder; 
private String path; 
private Bundle extras; 
private boolean mIsVideoSizeKnown = false; 
private boolean mIsVideoReadyToBePlayed = false;  

protected void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.mediaplayer_2); 
    mPreview = (SurfaceView) findViewById(R.id.surface); 
    holder = mPreview.getHolder(); 
    holder.addCallback(this); 
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    extras = getIntent().getExtras(); 
} 

private void playVideo(String movieUrl) { 
     doCleanUp(); 
     try { 
      path=movieUrl; 


      // Create a new media player and set the listeners 
      mMediaPlayer = new MediaPlayer(); 
      mMediaPlayer.setDataSource(path); 
      mMediaPlayer.setDisplay(holder); 
      mMediaPlayer.prepare(); 
      mMediaPlayer.setOnBufferingUpdateListener(this); 
      mMediaPlayer.setOnCompletionListener(this); 
      mMediaPlayer.setOnPreparedListener(this); 
      mMediaPlayer.setOnVideoSizeChangedListener(this); 
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 


     } catch (Exception e) { 
      Log.e(TAG, "error: " + e.getMessage(), e); 
      return false; 
     } 
    } 

    public void onBufferingUpdate(MediaPlayer arg0, int percent) { 
     Log.d(TAG, "onBufferingUpdate percent:" + percent); 

    } 

    public void onCompletion(MediaPlayer arg0) { 
     Log.d(TAG, "onCompletion called"); 
    } 


    private void doCleanUp() { 
     mVideoWidth = 0; 
     mVideoHeight = 0; 
     mIsVideoReadyToBePlayed = false; 
     mIsVideoSizeKnown = false; 
    } 

    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { 
     Log.v(TAG, "onVideoSizeChanged called"); 
     if (width == 0 || height == 0) { 
      Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")"); 
      return; 
     } 
     mIsVideoSizeKnown = true; 
     mVideoWidth = width; 
     mVideoHeight = height; 
     if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) { 
      startVideoPlayback(); 
     } 
    } 

    public void onPrepared(MediaPlayer mediaplayer) { 
     Log.d(TAG, "onPrepared called"); 
     mIsVideoReadyToBePlayed = true; 
     if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) { 
      startVideoPlayback(); 
     } 
    } 

    public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) { 
     Log.d(TAG, "surfaceChanged called"); 

    } 

    public void surfaceDestroyed(SurfaceHolder surfaceholder) { 
     Log.d(TAG, "surfaceDestroyed called"); 
    } 


    public void surfaceCreated(SurfaceHolder holder) { 
     Log.d(TAG, "surfaceCreated called"); 
     playVideo(extras.getString(MOVIEURL)); 



    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     releaseMediaPlayer(); 
     doCleanUp(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     releaseMediaPlayer(); 
     doCleanUp(); 
    } 

    private void releaseMediaPlayer() { 
     if (mMediaPlayer != null) { 
      mMediaPlayer.release(); 
      mMediaPlayer = null; 
     } 
    } 

    private void startVideoPlayback() { 
     Log.v(TAG, "startVideoPlayback"); 
     holder.setFixedSize(mVideoWidth, mVideoHeight); 
     mMediaPlayer.start(); 
    } 

此代碼工作正常,但整個影片被緩衝之前不打碼。

無論如何,我們可以在後臺緩衝視頻的同時播放視頻。

謝謝。

+1

使用['AsyncTask'](http://developer.android.com/reference/android/os/AsyncTask.html)。 – 2012-07-11 11:38:43

+0

使用AsyncTask顯示進度對話框。你能告訴我,用它來流式傳輸嗎? – 2012-07-11 11:43:23

+1

刪除'onPrepared()'中的'startVideoPlayback();'並在'playVideo()'中調用它,它會解決你的問題。 – yorkw 2012-07-11 22:59:22

回答

1

代替

mMediaPlayer.prepare(); 

使用:

mMediaPlayer.prepareAsync();