In this diagram are lot of states:
but with standard listener I can listen just some basic states.
So is possible to listen every states changes for MediaPlayer?
You can't destroy one activity from another activity. But you can pause the media player by creating a new instance of MediaPlayer in the second activity and callind the 'stop' method !
setOnCheckChangedListener(null); This should work for any event listener.
To go to the preparing state from initialized state, you can call the prepareAsync() method which transfers the object to preparing state and when the preparation is done then the onPrepared() method of the OnPreparedListener interface is called and the object is transferred to the prepared state.
mediaPlayer. start(); mediaPlayer. pause(); On call to start() method, the music will start playing from the beginning.
Unfortunately I found no way of doing this other than surrounding the MediaPlayer with a custom state machine container.
Added ...
You could create a container that is a singleton and defines it's own states. And the only interaction with the MediaPlayer is done through this container. All other classes need to access the API of the container.
Below is a simple outline:
public class MyContainer implements OnBufferingUpdateListener,
OnCompletionListener,
OnErrorListener,
OnInfoListener,
OnPreparedListener
{
static enum MP_STATES
{
MPS_IDLE,
MPS_INITIALIZED,
MPS_PREPARING,
MPS_PREPARED,
MPS_STARTED,
MPS_STOPPED,
MPS_PAUSED,
MPS_PLAYBACK_COMPLETED,
MPS_ERROR,
MPS_END,
}
private static MyContainer s_mpm = null;
private MP_STATES m_eState;
private MediaPlayer m_mp;
public static MyContainer get()
{
if (null == s_mpm)
{
s_mpm = new MyContainer();
}
return s_mpm;
}
private MyContainer()
{
m_mp = new MediaPlayer();
m_mp.setOnBufferingUpdateListener(this);
m_mp.setOnCompletionListener(this);
m_mp.setOnErrorListener(this);
m_mp.setOnInfoListener(this);
m_mp.setOnPreparedListener(this);
m_eState = MP_STATES.MPS_IDLE;
}
public MP_STATES getState()
{
return m_eState;
}
public void setUrl(String szUrl)
{
bringToIdleState();
try {
m_mp.setDataSource(szUrl);
m_eState = MP_STATES.MPS_INITIALIZED;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
m_mp.prepareAsync();
m_eState = MP_STATES.MPS_PREPARING;
}
public void play()
{
switch (m_eState)
{
case MPS_PREPARED:
case MPS_PAUSED:
m_mp.start();
m_eState = MP_STATES.MPS_STARTED;
break;
default:
// !! throw exception
}
}
public void pause()
{
switch (m_eState)
{
case MPS_STARTED:
m_mp.pause();
m_eState = MP_STATES.MPS_PAUSED;
break;
default:
// !! throw exception
}
}
public void release()
{
m_mp.release();
m_mp = null;
}
private void bringToIdleState()
{
// reset() should bring MP to IDLE
m_mp.reset();
m_eState = MP_STATES.MPS_IDLE;
}
// ** Callbacks
@Override
public void onPrepared(MediaPlayer mp)
{
m_eState = MP_STATES.MPS_PREPARED;
m_mp.start();
m_eState = MP_STATES.MPS_STARTED;
}
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra)
{
return false;
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra)
{
m_eState = MP_STATES.MPS_IDLE;
return false;
}
@Override
public void onCompletion(MediaPlayer mp)
{
// play the next song
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With