2012-01-03 69 views
2

中調用,每當我開始我的流,我得到兩個MediaPlayer錯誤。Mediaplayer錯誤:開始在狀態4

01-03 12:04:35.738: D/PlayerActivity(10164): play
01-03 12:04:35.742: D/PlayerService(10164): setAudiotStreamType
01-03 12:04:35.742: D/PlayerService(10164): setDataSource
01-03 12:04:35.742: D/PlayerService(10164): prepareAsync
01-03 12:04:35.742: E/MediaPlayer(10164): start called in state 4
01-03 12:04:35.742: E/MediaPlayer(10164): error (-38, 0)
01-03 12:04:35.742: D/PlayerService(10164): onPrepared
01-03 12:04:35.742: D/PlayerService(10164): onCreate Service
01-03 12:04:35.750: E/MediaPlayer(10164): Error (-38,0)
01-03 12:04:38.261: D/PlayerService(10164): onPrepared

在調試標籤「播放」中,我點擊了播放按鈕。 比調用MediaPlayer服務中的「onCreate」,它執行「setAudioStreamTpe」,「setDataSource」,「prepareAsync」,然後onPrepared(myMediaPlayer)。 OnPrepared僅執行mp.start()。之後記錄「onPrepared」。然後,在我的服務的「onCreate」結尾,記錄「onCreate Service」。

爲什麼有2準備好了?用mp.prepare()我沒有錯誤。

驗證碼:

@Override 
public void onCreate() { 
    mp = new MediaPlayer(); 
    mp.setOnPreparedListener(this); 
    prepareMediaPlayer(); 
    onPrepared(mp); 
    Log.d(TAG, "onCreate Service"); 
} 


    public void prepareMediaPlayer() 
{ 
    try { 
     mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     Log.d(TAG, "setAudiotStreamType"); 
     mp.setDataSource(PlayerActivity.soundUrl); 
     Log.d(TAG, "setDataSource"); 
     mp.prepareAsync(); 
     Log.d(TAG, "prepareAsync"); 

    } 
    catch (IOException e) {} 
    catch (IllegalArgumentException e) {} 
    catch (IllegalStateException e) {} 

} 


    @Override 
public void onPrepared(MediaPlayer mp) { 
    mp.start(); 
    if (PlayerService.mp.isPlaying() == true){ 
     notification(); 
     PlayerActivity.mpState = PlayerActivity.PLAYING; 
    } 
    Log.d(TAG, "onPrepared"); 
} 
+0

請提供代碼本身。 – MByD 2012-01-03 11:18:57

+0

已添加代碼。希望能幫助到你! – Leandros 2012-01-03 11:25:32

回答

6

你不應該調用onCreate()onPrepared(mp);,它會被稱爲一旦MediaPlayer的準備。

public void onCreate() { 
    mp = new MediaPlayer(); 
    mp.setOnPreparedListener(this); 
    prepareMediaPlayer(); 
    Log.d(TAG, "onCreate Service"); 
} 
+0

嗯。很簡單。謝謝! :) – Leandros 2012-01-03 12:05:34

0

狀態4指Mediaplayer處於準備狀態

,我們調用其他操作,如

Mediaplayer.start() 
Mediaplayer.stop() 
Mediaplayer.pause() 

或任何其他事情。

根據你的代碼是Onprepared();

你可以看到,調用

Mediaplayer.prepare() 

Mediaplayer.prepareAsync() 

後,你不需要調用

Mediaplayer.Onprepared() 

它將調用onPrepares一旦MediaPlayer正在準備 只是刪除

onPrepared(mp); 

prepareMediaPlayer(); 

你會好到哪裏去

相關問題