What are the correct arguments for requestaudiofocus?

I am new to Android and Java. I've worked with the MediaPlayer and AudioManager examples provided by Android developers and other websites.

I noticed that the call to requestAudioFocus () seems to be using two separate signatures. For example, from the site http://developer.android.com/guide/topics/media/mediaplayer.html there is:

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
    AudioManager.AUDIOFOCUS_GAIN);

if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    // could not get audio focus.
}

      

With the following text:

"The first parameter of requestAudioFocus () is AudioManager.OnAudioFocusChangeListener, the onAudioFocusChange () method is called whenever the audio focus changes, so you must also implement this interface in your services and actions. For example:" (With the following code :)

class MyService extends Service
                implements AudioManager.OnAudioFocusChangeListener {
    // ....
    public void onAudioFocusChange(int focusChange) {
        // Do something based on focus change...
    }
}

      

Then from the site: http://developer.android.com/training/managing-audio/audio-focus.html there is:

AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
...

// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
                                 // Use the music stream.
                                 AudioManager.STREAM_MUSIC,
                                 // Request permanent focus.
                                 AudioManager.AUDIOFOCUS_GAIN);

if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    am.registerMediaButtonEventReceiver(RemoteControlReceiver);
    // Start playback.
}

      

from:

OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
        if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT
            // Pause playback
        } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            // Resume playback 
        } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
            am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
            am.abandonAudioFocus(afChangeListener);
            // Stop playback
        }
    }
};

      

I've seen this dichotomy on many sites, providing sample code for handling audio focus changes. I understand that "this" provides the context for the current state of the application. I don't understand why in some cases "this" is the correct parameter, while in other cases the change listener handle is the correct parameter when calling requestAudioFocus ().

In fact, the first example I have provided indicates that the first parameter should be AudioManager.OnAudioFocusChangeListener. But "this" is used.

If you could explain why "this" is used instead of AudioManager.OnAudioFocusChangeListener is used as a parameter, it would be quite helpful.

+3


source to share


1 answer


It always takes onAudioFocusedChangeListener. Where this is passed, the current class implements the onAudioFocusChangeListener interface.



+3


source







All Articles