Sto provando a mettere in pausa il lettore multimediale quando suona il telefono. Io uso il codice di esempio dal sito di Android. È così;AUDIOFOCUS_LOSS chiamato dopo una telefonata in 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;
}
}
Quando il telefono squilla AUDIOFOCUS_LOSS_TRANSIENT viene inviato; che è OK. E quando la chiamata termina, viene inviato AUDIOFOCUS_GAIN e il giocatore continua a giocare; che è anche OK. Ma subito dopo aver inviato AUDIOFOCUS_GAIN, viene inviato AUDIOFOCUS_LOSS. Hai qualche idea sul perché sta perdendo l'audio focus? Grazie in anticipo.
Ho aggiunto una taglia a questa domanda perché a volte ho visto la stessa cosa. O ottengo un ciclo di AUDIOFOCUS_GAIN => AUDIOFOCUS_LOSS => AUDIOFOCUS_GAIN => AUDIOFOCUS_LOSS => AUDIOFOCUS_GAIN => AUDIOFOCUS_LOSS => ... ecc. – StackOverflowed
Quale dispositivo usi (prova con l'emulatore)? Ha la musica di google? Puoi verificare che funzioni con un altro lettore musicale (ad esempio, google music)? Aggiungi un logcat. Ci possono essere cose interessanti lì. Potrebbe esserci qualche altra (vecchia) app musicale che ascolti la fine della chiamata e "ruba" l'attenzione? –
Ho anche lo stesso problema: il mio dispositivo è Galaxy Note N7000 - Android 4.0.3. E un'altra cosa: dopo che AUDIOFOCUS_LOSS è stato attivato, il listener non viene mai richiamato! – hungson175