Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

State change listener for MediaPlayer

In this diagram are lot of states:

MediaPlayer states

but with standard listener I can listen just some basic states.

So is possible to listen every states changes for MediaPlayer?

like image 777
neworld Avatar asked Oct 27 '12 16:10

neworld


People also ask

How can I stop MediaPlayer in another activity?

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 !

How do I turn off listener on Android?

setOnCheckChangedListener(null); This should work for any event listener.

Which method is used to bring the MediaPlayer to prepared state?

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.

What method is used to call media?

mediaPlayer. start(); mediaPlayer. pause(); On call to start() method, the music will start playing from the beginning.


1 Answers

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) 
    {
    }

}

}

like image 138
paiego Avatar answered Sep 21 '22 04:09

paiego