@@ -83,6 +83,8 @@ public final class BackgroundPlayer extends Service {
8383 //////////////////////////////////////////////////////////////////////////*/
8484 private static final int NOTIFICATION_ID = 123789 ;
8585
86+ private boolean shouldUpdateNotification ;
87+
8688 private NotificationManager notificationManager ;
8789 private NotificationCompat .Builder notBuilder ;
8890 private RemoteViews notRemoteView ;
@@ -150,16 +152,27 @@ private void onClose() {
150152
151153 private void onScreenOnOff (boolean on ) {
152154 if (DEBUG ) Log .d (TAG , "onScreenOnOff() called with: on = [" + on + "]" );
153- if (on ) {
154- if (basePlayerImpl .isPlaying () && !basePlayerImpl .isProgressLoopRunning ()) basePlayerImpl .startProgressLoop ();
155- } else basePlayerImpl .stopProgressLoop ();
155+ shouldUpdateNotification = on ;
156156
157+ if (on ) {
158+ if (basePlayerImpl .isPlaying () && !basePlayerImpl .isProgressLoopRunning ()) {
159+ basePlayerImpl .startProgressLoop ();
160+ }
161+ } else {
162+ basePlayerImpl .stopProgressLoop ();
163+ }
157164 }
158165
159166 /*//////////////////////////////////////////////////////////////////////////
160167 // Notification
161168 //////////////////////////////////////////////////////////////////////////*/
162169
170+ private void resetNotification () {
171+ if (shouldUpdateNotification ) {
172+ notBuilder = createNotification ();
173+ }
174+ }
175+
163176 private NotificationCompat .Builder createNotification () {
164177 notRemoteView = new RemoteViews (BuildConfig .APPLICATION_ID , R .layout .player_notification );
165178 bigNotRemoteView = new RemoteViews (BuildConfig .APPLICATION_ID , R .layout .player_notification_expanded );
@@ -214,7 +227,7 @@ private void setupNotification(RemoteViews remoteViews) {
214227 */
215228 private synchronized void updateNotification (int drawableId ) {
216229 //if (DEBUG) Log.d(TAG, "updateNotification() called with: drawableId = [" + drawableId + "]");
217- if (notBuilder == null ) return ;
230+ if (notBuilder == null || ! shouldUpdateNotification ) return ;
218231 if (drawableId != -1 ) {
219232 if (notRemoteView != null ) notRemoteView .setImageViewResource (R .id .notificationPlayPause , drawableId );
220233 if (bigNotRemoteView != null ) bigNotRemoteView .setImageViewResource (R .id .notificationPlayPause , drawableId );
@@ -267,6 +280,7 @@ private class BasePlayerImpl extends BasePlayer {
267280 public void handleIntent (Intent intent ) {
268281 super .handleIntent (intent );
269282
283+ shouldUpdateNotification = true ;
270284 notBuilder = createNotification ();
271285 startForeground (NOTIFICATION_ID , notBuilder .build ());
272286
@@ -276,6 +290,7 @@ public void handleIntent(Intent intent) {
276290
277291 @ Override
278292 public void initThumbnail (final String url ) {
293+ resetNotification ();
279294 if (notRemoteView != null ) notRemoteView .setImageViewResource (R .id .notificationCover , R .drawable .dummy_thumbnail );
280295 if (bigNotRemoteView != null ) bigNotRemoteView .setImageViewResource (R .id .notificationCover , R .drawable .dummy_thumbnail );
281296 updateNotification (-1 );
@@ -288,7 +303,7 @@ public void onThumbnailReceived(Bitmap thumbnail) {
288303
289304 if (thumbnail != null ) {
290305 // rebuild notification here since remote view does not release bitmaps, causing memory leaks
291- notBuilder = createNotification ();
306+ resetNotification ();
292307
293308 if (notRemoteView != null ) notRemoteView .setImageViewBitmap (R .id .notificationCover , thumbnail );
294309 if (bigNotRemoteView != null ) bigNotRemoteView .setImageViewBitmap (R .id .notificationCover , thumbnail );
@@ -335,6 +350,7 @@ public void onRepeatClicked() {
335350
336351 @ Override
337352 public void onUpdateProgress (int currentProgress , int duration , int bufferPercent ) {
353+ resetNotification ();
338354 if (bigNotRemoteView != null ) {
339355 if (currentInfo != null ) {
340356 bigNotRemoteView .setTextViewText (R .id .notificationSongName , getVideoTitle ());
@@ -400,6 +416,7 @@ public void onError(Exception exception) {
400416 public void sync (@ Nullable final StreamInfo info ) {
401417 super .sync (info );
402418
419+ resetNotification ();
403420 notRemoteView .setTextViewText (R .id .notificationSongName , getVideoTitle ());
404421 notRemoteView .setTextViewText (R .id .notificationArtist , getUploaderName ());
405422 bigNotRemoteView .setTextViewText (R .id .notificationSongName , getVideoTitle ());
0 commit comments