Code example for MediaPlayer

Methods: setOnCompletionListenersetOnErrorListenersetOnPreparedListenersetWakeMode

0
     
    private final IBinder mBinder = new MusicServiceBinder();
     
    void createMediaPlayerIfNeeded() { 
        if (mMediaPlayer == null) {
        	mMediaPlayer = new MediaPlayer();
 
            // Make sure the media player will acquire a wake-lock while playing. If we don't do 
            // that, the CPU might go to sleep while the song is playing, causing playback to stop. 
            // 
            // Remember that to use this, we have to declare the android.permission.WAKE_LOCK 
            // permission in AndroidManifest.xml. 
        	mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
 
            // we want the media player to notify us when it's ready preparing, and when it's done 
            // playing: 
        	mMediaPlayer.setOnPreparedListener(this);
        	mMediaPlayer.setOnCompletionListener(this);
        	mMediaPlayer.setOnErrorListener(this);
        } 
        else 
        	mMediaPlayer.reset();
    }