Estoy intentando pausar el reproductor multimedia cuando suena el teléfono. Uso el código de muestra del sitio de Android. Es como esto;AUDIOFOCUS_LOSS llamado después de una llamada telefónica en android
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
// resume playback
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
mMediaPlayer.setVolume(1.0f, 1.0f);
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
// Lost focus for an unbounded amount of time: stop playback and
// release media player
stopMediaPlayer();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// Lost focus for a short time, but we have to stop
// playback. We don't release the media player because playback
// is likely to resume
if (mMediaPlayer.isPlaying())
mMediaPlayer.pause();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// Lost focus for a short time, but it's ok to keep playing
// at an attenuated level
if (mMediaPlayer.isPlaying())
mMediaPlayer.setVolume(0.1f, 0.1f);
break;
}
}
Cuando suena el teléfono, se envía AUDIOFOCUS_LOSS_TRANSIENT; lo cual está bien Y cuando finaliza la llamada, se envía AUDIOFOCUS_GAIN y el jugador continúa jugando; que también está bien. Pero justo después de enviar AUDIOFOCUS_GAIN, se envía AUDIOFOCUS_LOSS. ¿Tiene alguna idea de por qué está perdiendo el enfoque de audio? Thx por adelantado.
He añadido una recompensa a esta pregunta porque he visto lo mismo a veces. O me sale un bucle de AUDIOFOCUS_GAIN => AUDIOFOCUS_LOSS => AUDIOFOCUS_GAIN => AUDIOFOCUS_LOSS => AUDIOFOCUS_GAIN => AUDIOFOCUS_LOSS => ... etc. – StackOverflowed
¿Qué dispositivo usa (prueba con el emulador)? ¿Tiene google music? ¿Puedes verificar que funcione con otro reproductor de música (por ejemplo, google music)? Añade un logcat. Puede haber cosas interesantes allí. ¿Podría haber alguna otra aplicación de música (antigua) que escuche que la llamada finalice y "robe" el foco? –
También tengo el mismo problema: mi dispositivo es Galaxy Note N7000 - android 4.0.3. Y una cosa más: después de que AUDIOFOCUS_LOSS se active, ¡al oyente nunca más se le llamará! – hungson175