From 5cd7d7f7a4525d8c277f307ccc79dbde3af3fa08 Mon Sep 17 00:00:00 2001 From: Jean-Michel Trivi Date: Thu, 16 Mar 2017 18:38:50 -0700 Subject: [PATCH] NotificationPlayer: fix error listener, sync in playback thread The error and completion listeners should be set before MediaPlayer.prepare() is called, in case of an error during prepare. Move the sleep to synchronize the playback of the notification and the ducking from the MediaPlayer to the playback thread. Test: play a notification during media playback Bug: 35855841 Change-Id: I42f01f485f533c1cc84b9d927f0dc594cdb7ec8f --- .../android/systemui/media/NotificationPlayer.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java b/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java index 5cd7e41167f18..0a2ff2e4119e3 100644 --- a/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java +++ b/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java @@ -91,6 +91,8 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener player.setAudioAttributes(mCmd.attributes); player.setDataSource(mCmd.context, mCmd.uri); player.setLooping(mCmd.looping); + player.setOnCompletionListener(NotificationPlayer.this); + player.setOnErrorListener(NotificationPlayer.this); player.prepare(); if ((mCmd.uri != null) && (mCmd.uri.getEncodedPath() != null) && (mCmd.uri.getEncodedPath().length() > 0)) { @@ -118,12 +120,15 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener // can lead to AudioFocus being released too early, before the second sound is // done playing. This class should be modified to use a single thread, on which // command are issued, and on which it receives the completion callbacks. - player.setOnCompletionListener(NotificationPlayer.this); - player.setOnErrorListener(NotificationPlayer.this); if (DEBUG) { Log.d(mTag, "notification will be delayed by " + mNotificationRampTimeMs + "ms"); } - player.setStartDelayMs(mNotificationRampTimeMs); - player.start(); + try { + Thread.sleep(mNotificationRampTimeMs); + player.start(); + } catch (InterruptedException e) { + Log.e(mTag, "Exception while sleeping to sync notification playback " + + " with ducking", e); + } if (mPlayer != null) { mPlayer.release(); }