Code example for MediaPlayer

Methods: setOnErrorListenersetVolume

0
        if (mState == BellRepeaterState.INITIAL) {
 
            // Initialise the MediaPlayer 
            mMediaPlayer = MediaPlayer.create(mContext, mSoundInfo.getSoundResid());
            // Set to maximum volume possible (it's really soft!) 
            mMediaPlayer.setVolume(1, 1);
            // On Error, release it and shut it down and put it away. 
            // But log a message so that we know... 
            mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
 
                @Override 
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Log.e("BellRepeater", "The media player went into an errored state! Releasing.");
                    // The MediaPlayer coming here should be the same one as mMediaPlayer in the BellRepeater class 
                    if (mp != mMediaPlayer){
                        Log.e(this.getClass().getSimpleName(), "OnErrorListener mp wasn't the same as mMediaPlayer!");
                    } 
                    mMediaPlayer.release();
                    mMediaPlayer = null;
                    return false; 
                }