Code example for MediaPlayer

Methods: setOnErrorListenersetVolume

        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() {
                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 = null;
                    return false; 
Contextual code suggestions in your IDE  Get Codota for Java