Answers for "how to request audio focus android"

4

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.
}
Posted by: Guest on November-09-2020

Code answers related to "how to request audio focus android"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language