Jacek Włodarczyk
Jacek Włodarczyk

Reputation: 31

Android Media Player avoid audio focus change when starting video

If i play music in any Android music player then start my app with intro video (handled with MediaPlayer) music stops. I want to avoid that (music keep playing) because my app intro video has no sound itself.

Is it possible to avoid getting audio focus for MediaPlayer when it starts it's content? (mediaPlayer.start() --> AudioManager dispatching onAudioFocusChange for Music Player)

Upvotes: 3

Views: 1569

Answers (1)

Davendar Ojha
Davendar Ojha

Reputation: 11

Yes it is possible Using AudioManager requestAudioFocus to capture audio focus back. Please make sure you check the result and start playing music iff it is success.

AudioManager.OnAudioFocusChangeListener afChangeListener;    
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    int result = am.requestAudioFocus(audioFocusChangeListener,
                    // Use the music stream.
                    AudioManager.STREAM_MUSIC,
                    // Request permanent focus.
                    AudioManager.AUDIOFOCUS_GAIN);

Upvotes: 1

Related Questions