Code example for MediaPlayer

Methods: setOnCompletionListenersetOnErrorListenersetOnPreparedListenersetWakeMode

0
     * if needed. reset the existing media player if one already exists. 
     */ 
    protected void createMediaPlayerIfNeeded() { 
        if (mPlayer == null) {
            mPlayer = new MediaPlayer();
 
            // make sure the CPU won't go to sleep while media is playing 
            mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
 
            // the media player will notify the service when it's ready preparing, and when it's done playing 
            mPlayer.setOnPreparedListener(this);
            mPlayer.setOnCompletionListener(this);
            mPlayer.setOnErrorListener(this);
             
        } else { 
            mPlayer.reset();
        } 
    } 
 
    /** 
     * Processes a request to pause the current playback