简体   繁体   中英

Start called in state 4 error in android MediaPlayer?

I am starting playback of an online audio stream in a service. This is the method which starts the playback:

    void start1(String a){
        try{
        mp.setDataSource(a);//mp is a global MediaPlayer variable
        mp.setOnPreparedListener(this);
        mp.prepareAsync();
        mp.start();
        }catch(Exception e){
            noerror=false;
                }
  }

The onPrepared() function:

    @Override
  public void onPrepared(MediaPlayer mp1){
        mp.start();
    }

This code gives me the error

Start called in state 4: error(-38,0)

What is wrong with this code?

Remove this line

mp.start();

from your start1 method since you're already starting playing in the onPrepared method.

State 4 means Mediaplayer is in preparing state

and we call other actions like

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

or any other.

As per your code, it is Mediaplayer.start();

Once Onprepared() method is called you can do the further processing.

Just remove

mp.start() 

after

mp.prepareAsync();

And you will be good to go!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM