request audio focus android code example

Example: request audio focus android

//set the listener (this should be global)
AudioManager.OnAudioFocusChangeListener audioListener = new AudioManager.OnAudioFocusChangeListener() {
	@Override
	public void onAudioFocusChange(int i) {
		//code to hundle diffrent kind of focus states.
	}
};



//set the request
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(audioListener , AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
	// You now have the audio focus and may play sound.
}else if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
	// Handle the failure.
}

Tags:

Misc Example