2012-02-24 80 views
3

我剛剛花了大約2個小時試圖讓這個工作。我從來沒有得到過使用MediaPlayer播放視頻的好運氣。我在這裏做錯了什麼?它通過第一次播放音頻,沒有視頻。然後第二次,當我嘗試重新登錄IllegalStateException時,setDataSource。我試過在撥打電話前已嘗試撥打reset()來電和release()onCompletion。我只是得到不同的StateExceptions(0,64,128)我已經用完了想法。MediaPlayer視頻問題

private void playVideo() { 
    mMediaPlaying = true; 
    sv.setVisibility(View.VISIBLE); //surfaceview 
    try { 
     if(mp.isPlaying()) { 
      mp.stop(); 
      mp.reset(); 
     } 
     mp.setDisplay(sh); //surfaceholder 
     mp.setOnCompletionListener(this); 
     mp.setOnPreparedListener(this); 

     mp.setDataSource(getBaseContext(), Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test)); 
     mp.prepare(); 
     //mp.start(); 
    } catch (IllegalArgumentException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SecurityException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 


@Override 
public void onPrepared(MediaPlayer mp) { 
    Log.d("", "mp prepared"); 
    mp.start(); 

} 

@Override 
public void onCompletion(MediaPlayer arg0) { 
    sv.setVisibility(View.GONE); 
    mp.stop(); 
    mp.release(); 
    mMediaPlaying = false; 
    Log.d("", "Done playing media"); 

} 
+0

好吧,我刪除了,如果(mp.isPlaying()),並添加mp.reset(),並播放音頻的第一次,第二它播放一秒鐘的音頻,然後我得到「媒體服務器死亡」「錯誤(100,0)」「錯誤(100,0)」 – bwoogie 2012-02-24 03:58:44

回答

0

好吧,我基本上是複製/粘貼this here的生命週期,它似乎是工作.. 。除了第一次播放還沒有視頻...但我擺脫了錯誤:)

3

如果你想播放視頻,你可以使用VideoView。

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.html

你也可以使用的MediaController來控制視頻,如播放,暫停,快進和快退。

檢查此鏈接例如。

http://android-coding.blogspot.in/2011/03/using-videoview-to-play-mp4-from-sdcard.html

+0

我不想要控件,我需要OnCompletionListener哪個videoview讓你設置,但它似乎沒有工作... – bwoogie 2012-02-24 05:13:27

+0

您可以在VideoView中使用Oncompletion偵聽器 – John 2012-02-24 06:18:03

0

試試這個:

private void playVideo() { 
mMediaPlaying = true; 
sv.setVisibility(View.VISIBLE); //surfaceview 
try { 
    if(mp != null) { 
     mp.stop(); 
     mp.release(); 
    } 
    mp = new MediaPlayer(); 
    mp.setDisplay(sh); //surfaceholder 
    mp.setOnCompletionListener(this); 
    mp.setOnPreparedListener(this); 

    mp.setDataSource(getBaseContext(), Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test)); 
    mp.prepare(); 
    mp.start();