Media Player Called in State 0, Error (-38,0)

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();
}

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.

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.

Error: mediaplayer start called in state 0 error(-38,0)

You need to wait till the media player is prepared before calling start

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

If you want to start the player on button click. You can have a flag. boolean flag=false;

Then

 public void onPrepared(MediaPlayer player) 
{
flag= true;
}

Then in onClick

if(flag ==true)
{
mp.start();
}else
{
// media player not prepared
}

Error while trying to start media player: start called in state error (-38, 0)

This error usually means you are trying to call a function while in wrong state, for example calling seek while media player not ready yet.
Try to add mpApple.setOnPreparedListener() listener then called start() once it is prepared, also you will need to release the player once you are done as they recommend in documentation, so I suggest you make one MediaPlayer instance, and stop + release it before using it to play new audio file. for example

 public class HomeEnglishFoodsActivity extends ActionBarActivity
{
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
{
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View int position, long id) {
switch (position)
{
case 0:
if(player1 !=null)
{
player1.stop();
player1.release();
}
player1.create(this, R.raw.apple).setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer)
{
mediaPlayer.start();
}
});
}
}
});
}
}

MediaPlayer start called in state 0 (Error -38, 0), android?

You are trying to start media player, when it is not yet prepared. Try to use OnPreparedListener(), and start the player when it's ready, it should look somewhat like this:

yourMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer player) {
player.start();
}
});

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;
}

Android MediaPlayer Error (-38,0)

error 38 means you are asking the MediaPlayer to do something when in the wrong state. You won't be able to fathom your error just from this.

Look at your Logcat before and after this error code and see what else has gone wrong. It's more than like you are calling start before the MediaPlayer has prepared or some other error of state.

see http://developer.android.com/reference/android/media/MediaPlayer.html#StateDiagram

state machine

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

For some reason, changing where the onPreparedListener was fixed everything:

mediaPlayer.setOnPreparedListener(this);

Then inheriting the method and using:

@Override
public void onPrepared() {
Do stuff
}

This shouldn't have done anything, but in my case it worked.



Related Topics



Leave a reply



Submit