Chintan Khetiya Chintan Khetiya -4 years ago 171
Android Question

Getting Error in Media Player: SeekTo in wrong state: & start called in state 0

i am facing some issue in media player when i use as service.

1. If i initialize Media Player Object in Activity as static and further use in service class then its show me this

error (-38, 0)
,
Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0
,
start called in state 0
.

2. If i initialize Media Player Object in Service class and user in activity for more
action its show's me
Null Pointer
.

public static MediaPlayer mPlayer = null;


play Click Event

Audio_Player_Play.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub

if (mPlayer == null)
{

Intent Play_Intent = new Intent(RDS_Singel_Audio.this,
Audio_Player.class);

startService(Play_Intent);
Audio_Player_Play.setVisibility(View.GONE);
Audio_Player_Pause.setVisibility(View.VISIBLE);

} else
{
// Play_Streaming();
mPlayer.seekTo(mPlayer.getCurrentPosition());
mPlayer.start();
Audio_Player_Play.setVisibility(View.GONE);
Audio_Player_Pause.setVisibility(View.VISIBLE);
}
}
});


Pause Click Event

Audio_Player_Pause.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
// Play_Streaming();
// checking is medai player running

Audio_Player_Play.setVisibility(View.VISIBLE);
Audio_Player_Pause.setVisibility(View.GONE);

// checking is audio playing in background
if (mPlayer != null && mPlayer.isPlaying())
{
mPlayer.pause();
/*
* Intent Pause_Intent = new Intent(RDS_Singel_Audio.this,
* Audio_Player.class); stopService(Pause_Intent);
*/
Audio_Player_Play.setVisibility(View.VISIBLE);
Audio_Player_Pause.setVisibility(View.GONE);
}
}
});


OnResume() - OnPause() -OnDistroy()

@Override
protected void onResume()
{
super.onResume();
}
@Override
protected void onPause()
{
// cleanUp();
if (mPlayer != null && mPlayer.isPlaying())
{
mPlayer.release();
mPlayer = null;

}

super.onPause();
}
@Override
protected void onDestroy()
{
cleanUp();
super.onDestroy();
}

private void cleanUp()
{
if (mPlayer != null)
{
mVisualizerView.release();
mPlayer.release();
mPlayer = null;
}
}


Service.Java

public class Audio_Player extends Service
{
private static final String TAG = "MyService";

// public static MediaPlayer mPlayer;
Function f;
RDS_Singel_Audio sa;
MediaPlayer mPlayer;

@Override
public IBinder onBind(Intent intent)
{
return null;
}

@Override
public void onCreate()
{
try
{
sa = new RDS_Singel_Audio();
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG)
.show();

sa.mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
sa.mPlayer.setDataSource(sa.Audio_URL);
sa.mPlayer.prepare();// might take long! (for buffering, etc)
sa.mVisualizerView.link(sa.mPlayer);
sa.mPlayer.start();
sa.addLineRenderer();
sa.mPlayer.setLooping(false); // Set looping

// sa.mPlayer.start();

} catch (Exception e)
{
// TODO: handle exception
Log.d(TAG, "" + e);
}

}

@Override
public void onDestroy()
{
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
super.onDestroy();
if (sa.mPlayer != null)
{
sa.mPlayer.stop();
sa.mPlayer.release();

}
sa.mPlayer = null;

}

@Override
public void onStart(Intent intent, int startid)
{
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");

// mPlayer.setDataSource("http://clients.ncrypted.net/riddimapp/images/audio/4/df9919ccb2880933c795f43d735cf9bc.mp3");

}

}


I found some link about same issue but I don't know how solve it.

Answer Source

Well, I see this part of your code, but it is not clear what you are trying to achieve. You are trying to make the player go to its current position, but it is already at that position? You could remove that part then.

// Play_Streaming();
mPlayer.seekTo(mPlayer.getCurrentPosition());

Also, on your onpause you're releasing the player, but not generating it again on your onresume. You can recreate it there with code from that link you provided:

@Override
    protected void onResume()
    {
        super.onResume();
    }
@Override
    protected void onPause()
    {
        // cleanUp();
        if (mPlayer != null && mPlayer.isPlaying())
        {
            mPlayer.release();
            mPlayer = null;

        }

        super.onPause();
    }

Also, as you seem to be setting the source of the mediaplayer to be an url, you should change the prepare for a prepareAsync in the following part, so that it buffers:

sa.mPlayer.setDataSource(sa.Audio_URL);
sa.mPlayer.prepare();// might take long! (for buffering, etc)

The link you provided already has a lot of suggestions for overcoming these problems above. Please try the code on that link, especially the ones that suggest prepareAsync and the onprepared listener. Then you can get back here if it does not work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download