[Android] [PATCH 3/5] VideoPlayer: safer changeAudioFocus

Thomas Guillem thomas at gllm.fr
Tue Mar 17 10:14:32 CET 2015


It won't do anything if stopped or started a second time
---
 .../vlc/gui/video/VideoPlayerActivity.java         | 61 +++++++++++-----------
 1 file changed, 30 insertions(+), 31 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 5074454..3cd325a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -1203,41 +1203,40 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         if(!LibVlcUtil.isFroyoOrLater()) // NOP if not supported
             return AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
 
-        if (mAudioFocusListener == null) {
-            mAudioFocusListener = new OnAudioFocusChangeListener() {
-                @Override
-                public void onAudioFocusChange(int focusChange) {
+        mLostFocus = false;
+        int result;
+        if(acquire) {
+            if (mAudioFocusListener != null) {
+                mAudioFocusListener = new OnAudioFocusChangeListener() {
+                    @Override
+                    public void onAudioFocusChange(int focusChange) {
                     /*
                      * Pause playback during alerts and notifications
                      */
-                    switch (focusChange)
-                    {
-                        case AudioManager.AUDIOFOCUS_LOSS:
-                        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
-                        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
-                            if (mLibVLC.isPlaying()) {
-                                mLibVLC.pause();
-                                mLostFocus = true;
-                            }
-                            break;
-                        case AudioManager.AUDIOFOCUS_GAIN:
-                        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
-                        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
-                            if (!mLibVLC.isPlaying() && mLostFocus)
-                                mLibVLC.play();
-                            mLostFocus = false;
-                            break;
+                        switch (focusChange) {
+                            case AudioManager.AUDIOFOCUS_LOSS:
+                            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
+                            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
+                                if (mLibVLC.isPlaying()) {
+                                    mLibVLC.pause();
+                                    mLostFocus = true;
+                                }
+                                break;
+                            case AudioManager.AUDIOFOCUS_GAIN:
+                            case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
+                            case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
+                                if (!mLibVLC.isPlaying() && mLostFocus)
+                                    mLibVLC.play();
+                                mLostFocus = false;
+                                break;
+                        }
                     }
-                }
-            };
-        }
-
-        mLostFocus = false;
-        int result;
-        if(acquire) {
-            result = mAudioManager.requestAudioFocus(mAudioFocusListener,
-                    AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
-            mAudioManager.setParameters("bgm_state=true");
+                };
+                result = mAudioManager.requestAudioFocus(mAudioFocusListener,
+                        AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
+                mAudioManager.setParameters("bgm_state=true");
+            } else
+                result = AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
         }
         else {
             if (mAudioManager != null && mAudioFocusListener != null) {
-- 
2.1.3



More information about the Android mailing list