Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you can target API level 8 and above (Android 2.2), then have a look into the <a href="http://developer.android.com/reference/android/media/AudioManager.html" rel="noreferrer">AudioManager</a>, specifically <a href="http://developer.android.com/reference/android/media/AudioManager.html#requestAudioFocus(android.media.AudioManager.OnAudioFocusChangeListener,%20int,%20int)" rel="noreferrer"><code>requestAudioFocus()</code></a> with <code>AUDIOFOCUS_GAIN_TRANSIENT</code> and <code>AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK</code>, and <a href="http://developer.android.com/reference/android/media/AudioManager.html#abandonAudioFocus(android.media.AudioManager.OnAudioFocusChangeListener)" rel="noreferrer"><code>abandonAudioFocus()</code></a>.</p> <p>These give hints to the currently-playing music stream that your app would like to play a brief sound. If you use <code>AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK</code> with <code>requestAudioFocus()</code> you can get a nice effect where the already-playing music pauses, your sound plays, and then the playing music fades back in again when you call <code>abandonAudioFocus()</code>.</p> <p>This works nicely with the built-in media players on the 2.2 devices I've tried it with (Samsung Galaxy S Wi-Fi and HTC Wildfire.) I've not tried it extensively with other music-playing apps, but it definitely works with Spotify, at least, and is the official way of doing exactly what you want to do.</p> <p>Note that you'll need to work out for yourself when to call <code>abandonAudioFocus()</code> as most sounds are played asynchronously. Luckily, both <code>MediaPlayer</code> and <code>TextToSpeech</code> provide callback mechanisms so they can give you a prod when they've finished playing your sound (this <em>isn't</em> true of <code>SoundPool</code>, so that's more annoying to use audio focus with.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload