OSDN Git Service

NotificationPlayer: fix focus not abandoned on playback error
authorJean-Michel Trivi <jmtrivi@google.com>
Wed, 27 Dec 2017 19:55:30 +0000 (11:55 -0800)
committerJean-Michel Trivi <jmtrivi@google.com>
Wed, 27 Dec 2017 22:14:37 +0000 (14:14 -0800)
If an exception is fired when trying to start the MediaPlayer
  after audio focus was requested, it will not be abandoned
  on error or completion.

Bug: 70727414
Test: see bug
Change-Id: Ib2a07499829890a858d5a38d46c3575d2d7b6629

packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java

index b5c0d53..f5f06db 100644 (file)
@@ -133,12 +133,19 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener
                             + mNotificationRampTimeMs + "ms"); }
                     try {
                         Thread.sleep(mNotificationRampTimeMs);
-                        player.start();
                     } catch (InterruptedException e) {
                         Log.e(mTag, "Exception while sleeping to sync notification playback"
                                 + " with ducking", e);
                     }
-                    if (DEBUG) { Log.d(mTag, "player.start"); }
+                    try {
+                        player.start();
+                        if (DEBUG) { Log.d(mTag, "player.start"); }
+                    } catch (Exception e) {
+                        player.release();
+                        player = null;
+                        // playing the notification didn't work, revert the focus request
+                        abandonAudioFocusAfterError();
+                    }
                     if (mPlayer != null) {
                         if (DEBUG) { Log.d(mTag, "mPlayer.release"); }
                         mPlayer.release();
@@ -147,6 +154,8 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener
                 }
                 catch (Exception e) {
                     Log.w(mTag, "error loading sound for " + mCmd.uri, e);
+                    // playing the notification didn't work, revert the focus request
+                    abandonAudioFocusAfterError();
                 }
                 this.notify();
             }
@@ -154,6 +163,16 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener
         }
     };
 
+    private void abandonAudioFocusAfterError() {
+        synchronized (mQueueAudioFocusLock) {
+            if (mAudioManagerWithAudioFocus != null) {
+                if (DEBUG) Log.d(mTag, "abandoning focus after playback error");
+                mAudioManagerWithAudioFocus.abandonAudioFocus(null);
+                mAudioManagerWithAudioFocus = null;
+            }
+        }
+    }
+
     private void startSound(Command cmd) {
         // Preparing can be slow, so if there is something else
         // is playing, let it continue until we're done, so there