Code example for AudioManager

Methods: abandonAudioFocusunregisterMediaButtonEventReceiver

0
                AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    } 
 
    private boolean abandonAudioFocus() { 
        Log.d(TAG, "Abandoning audio focus.");
        return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == this.audioManager.abandonAudioFocus(this);
    } 
 
    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_GAIN:
                Log.d(TAG, "Re/gained focus.");
                this.startPlaybackAtFullVolume();
                break; 
            case AudioManager.AUDIOFOCUS_LOSS:
                Log.d(TAG, "Lost focus for an unbounded amount of time.");
                this.releaseMediaPlayer();
                break; 
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                Log.d(TAG, "Lost focus for a short time.");
                this.pauseMediaPlayer();
                break; 
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                Log.d(TAG, "Lost focus for a short time. Can duck.");
                this.lowerVolume();
                break; 
            default: 
                Log.w(TAG, "Unexpected onAudioFocusChange(" + focusChange + ")");
        } 
    } 
 
    private void startPlaybackAtFullVolume() { 
        if (this.mediaPlayer == null) {
            this.initMediaPlayer(this.lastIntent);
        } else if (!this.mediaPlayer.isPlaying()) {
            Log.d(TAG, "Resuming playback.");
            this.mediaPlayer.start();
        } else { 
            Log.d(TAG, "Going back to full volume.");
            this.mediaPlayer.setVolume(1.0f, 1.0f);
        } 
    } 
 
    private boolean lowerVolume() { 
        if (this.mediaPlayer != null && this.mediaPlayer.isPlaying()) {
            Log.d(TAG, "Lowering volume.");
            this.mediaPlayer.setVolume(0.1f, 0.1f);
            return true; 
        } else { 
            return false; 
        } 
    } 
 
    private boolean pauseMediaPlayer() { 
        if (this.mediaPlayer != null && this.mediaPlayer.isPlaying()) {
            Log.d(TAG, "Pausing playback.");
            this.mediaPlayer.pause();
            return true; 
        } else { 
            return false; 
        } 
    } 
 
    private void releaseMediaPlayer() { 
        if (this.mediaPlayer == null) {
            Log.d(TAG, "No media player. Nothing to release");
        } else { 
            Log.d(TAG, "Releasing audio player.");
            if (this.mediaPlayer.isPlaying()) {
                Log.d(TAG, "Stopping playback.");
                this.mediaPlayer.stop();
            } 
            this.mediaPlayer.release();
            this.mediaPlayer = null;
            this.abandonAudioFocus();
            Log.d(TAG, "Stopping foreground.");
            this.stopForeground(true);
            Log.d(TAG, "Unregistering noisy audio receiver.");
            super.unregisterReceiver(this.noisyAudioReceiver);
            Log.d(TAG, "Unregistering remote audio control receiver.");
            this.audioManager.unregisterMediaButtonEventReceiver(this.remoteControlReceiverName);
            Log.d(TAG, "Stopping service.");
            super.stopSelf(); 
        } 
    }