Leandros
Leandros

Reputation: 16825

Mediaplayer Error: start called in state 4

everytime I start my stream, I get two MediaPlayer Errors.

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

At the Debug Tag "play", I clicked the play button. Than my "onCreate" in the MediaPlayer service is called which executes "setAudioStreamTpe", "setDataSource", "prepareAsync", and then onPrepared(myMediaPlayer). OnPrepared only executes mp.start(). After that "onPrepared" is logged. And then, at the end of the "onCreate" from my service, "onCreate Service" is logged.

Why are there 2 onPrepared?! With mp.prepare() i got no errors.

The Code:

    @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");
}

Upvotes: 3

Views: 12722

Answers (2)

user6090074
user6090074

Reputation: 21

state 4 means Mediaplayer is in preparing state

and we call other actions like

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

or any other thing .

As per your code it is Onprepared();

You can see that after calling

Mediaplayer.prepare()

or

Mediaplayer.prepareAsync()

you do not need to call

Mediaplayer.Onprepared()

It will call onPrepares once MediaPlayer is ready just remove

onPrepared(mp);

after

prepareMediaPlayer();

And you will be good to go

Upvotes: 0

MByD
MByD

Reputation: 137322

You should not call onPrepared(mp); in onCreate(), it will be called once the MediaPlayer is ready.

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

Upvotes: 7

Related Questions