Android Mediaplayer Problems :"Error (-38 , 0) " and "Stop Called in State 1"

Media Player called in state 0, error (-38,0)

You need to call mediaPlayer.start() in the onPrepared method by using a listener.
You are getting this error because you are calling mediaPlayer.start() before it has reached the prepared state.

Here is how you can do it :

mp.setDataSource(url); 
mp.setOnPreparedListener(this);
mp.prepareAsync();

public void onPrepared(MediaPlayer player) {
player.start();
}

Android MediaPlayer Error (-38, 0) stop called in state 0

The problem is once you've stopped it, you setDataSource again. You have to then call prepare()

player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
player.prepare();

Anytime you call setDataSource() on it you will need to prepare() it again. Think of it as loading the file, getting ready for playback.

By the way I notice you mix up calling MediaPlayer.create() and also call setDataSource() multiple times for the same file. There's no need to do this. Once the MediaPlayer is created and prepared (which calling .create() does for you), you can just call .stop(), .pause(), .play() on it, no need to reset it all the time.

Android MediaPlayer start called in State 1. Error -38,0

Solved by myself

playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{

Uri myUri = Uri.parse("android.resource://" + v.getContext().getPackageName() + "/" + R.raw.sleep_away);
System.out.println(myUri);
playerM.setDataSource(v.getContext(), myUri);
playerM.prepare();
playerM.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer playerM){
playerM.start();
}
});
}catch(IOException e){
e.printStackTrace();
}

}
});

I know I should add other methods to change State, but the main purpose was to get it to play file.

Android MediaPlayer Problems :Error (-38 , 0) and stop called in state 1

Before prepare(), you need first to call setDataSource(..).

The Media framework is a very strict state machine, and it's really cumbersome to handle all the different states.

I've used this little wrapper that makes the coding/debugging a bit easier. You can give it a try.

Regarding emulator - note that not all file formats are supported on it.

Android MediaPlayer Error( start called in state 1; error (-38, 0); Error (-38,0))

You're taking a curious approach to the MediaPlayer, but one thing that jumps out at me is this:

public static MediaPlayer create(Context context, int musicID) {
if (mediaPlayer != null) {
mediaPlayer.release();
}
mediaPlayer.create(context, musicID);
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
return mediaPlayer.create(context, musicID);
}

You call mediaPlayer.create(), then .prepare(), then create(...) again?

Also, create is a static method, so it should be called as MediaPlayer.create and you should hold the reference to it. When you're calling prepare(), you're calling prepare() on a MediaPlayer object that hasn't been created.

Edit: If you want to keep this method, revise it accordingly:

public static MediaPlayer create(Context context, int musicID) {
if (mediaPlayer != null) {
mediaPlayer.release();
}
mediaPlayer = MediaPlayer.create(context, musicID);
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
return mediaPlayer;
}

mediaPlayer error -38,0

-38 refers to ENOSYS error code from errno.h (see this explanation https://stackoverflow.com/a/15206308/768935)

You seem to try to start the playing before the preparation is complete. Use the setOnPreparedListener() method to set a preparation listener and call the start() method only after the preparation is complete.

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.prepareAsync();

And remove the current mediaPlayer.start() invocation from the code.



Related Topics



Leave a reply



Submit