Interrupt MediaPlayer

My application makes a short announcement once in a while, either through TextToSpeechor using MediaPlayer. If there is any other music in the background, possibly using the application MediaPlayer, I would like to interrupt this music for a moment and after my ad resumes playing background music.

Is there any way to do this?

+3
source share
2 answers

If you can configure API level 8 or higher (Android 2.2), check out AudioManager , specifically requestAudioFocus()with AUDIOFOCUS_GAIN_TRANSIENTand AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCKand abandonAudioFocus().

, . AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK requestAudioFocus(), , , , , abandonAudioFocus().

2,2 , (Samsung Galaxy S Wi-Fi HTC Wildfire.) , Spotify, , , .

, , abandonAudioFocus(), . , MediaPlayer TextToSpeech , , ( SoundPool, .)

+5

, . , Android :

Intent i;
i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
context.sendBroadcast(i);

- "" 0 .

0

All Articles