[Android] audio: pause when audio focus is lost
Sébastien Toque
git at videolan.org
Sat Dec 28 12:00:14 CET 2013
vlc-ports/android | branch: master | Sébastien Toque <xilasz at gmail.com> | Sat Dec 28 11:53:44 2013 +0100| [991455572a1b113c29e77cc0bd0b51e157698e8b] | committer: Sébastien Toque
audio: pause when audio focus is lost
> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=991455572a1b113c29e77cc0bd0b51e157698e8b
---
vlc-android/src/org/videolan/vlc/AudioService.java | 41 +++++++++++++-------
1 file changed, 27 insertions(+), 14 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index b6677c3..9343955 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -263,21 +263,34 @@ public class AudioService extends Service {
if(!Util.isFroyoOrLater()) // NOP if not supported
return;
- audioFocusListener = new OnAudioFocusChangeListener() {
- @Override
- public void onAudioFocusChange(int focusChange) {
- if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK ||
- focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
- /*
- * Lower the volume to 36% to "duck" when an alert or something
- * needs to be played.
- */
- LibVLC.getExistingInstance().setVolume(36);
- } else {
- LibVLC.getExistingInstance().setVolume(100);
+ if (audioFocusListener == null) {
+ audioFocusListener = new OnAudioFocusChangeListener() {
+ @Override
+ public void onAudioFocusChange(int focusChange) {
+ LibVLC libVLC = LibVLC.getExistingInstance();
+ switch (focusChange)
+ {
+ case AudioManager.AUDIOFOCUS_LOSS:
+ if (libVLC.isPlaying())
+ libVLC.pause();
+ break;
+ case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
+ case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
+ /*
+ * Lower the volume to 36% to "duck" when an alert or something
+ * needs to be played.
+ */
+ libVLC.setVolume(36);
+ break;
+ case AudioManager.AUDIOFOCUS_GAIN:
+ case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
+ case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
+ libVLC.setVolume(100);
+ break;
+ }
}
- }
- };
+ };
+ }
AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
if(gain)
More information about the Android
mailing list