@@ -157,10 +157,13 @@ public void onPlay() {
157
157
//Should be started but sometimes not :s
158
158
if (!isServiceStarted ){
159
159
Log .d (TAG , "onPlay: Starting Service" );
160
- startService (new Intent (getApplicationContext (), MusicPlayerService .class ));
160
+ //startForegroundService(new Intent(getApplicationContext(), MusicPlayerService.class));
161
+ currentNotification = buildNotification ();
162
+ Log .d (TAG , "------------------------------------------onPLAY create notification-------------------------------------------" );
163
+ startForeground (NOTIFICATION_ID , currentNotification );
161
164
}
162
165
163
- if (mPlaybackState .getState () == PlaybackStateCompat .STATE_PAUSED ) { // If we are resuming something not trying to play an un-started song
166
+ if (mMediaController . getPlaybackState () .getState () == PlaybackStateCompat .STATE_PAUSED ) { // If we are resuming something not trying to play an un-started song
164
167
Log .d (TAG , "onPlay: Starting Player" );
165
168
166
169
//Doesn't update itself
@@ -170,17 +173,21 @@ public void onPlay() {
170
173
initNoisyReceiver ();
171
174
172
175
currentNotification = buildNotification ();
173
- mNotificationManager .notify (NOTIFICATION_ID , currentNotification );
176
+ Log .d (TAG , "------------------------------------------onPLAY Notify-------------------------------------------" );
177
+ startForeground (NOTIFICATION_ID , currentNotification );
174
178
}
175
179
176
180
177
181
178
- super .onPlay ();
182
+
183
+ //super.onPlay();
179
184
// TODO NOTE: onPlay is really onResume/onNowPlaying, don't try to play songs here
185
+ Log .d (TAG , "------------------------------------------onPLAY Notified-------------------------------------------" );
180
186
}
181
187
182
188
@ Override
183
189
public void onPause () {
190
+ Log .d (TAG , "------------------------------------------onPause-------------------------------------------" );
184
191
super .onPause ();
185
192
186
193
if ( mMediaPlayer .isPlaying () ) {
@@ -200,6 +207,7 @@ public void onPause() {
200
207
201
208
@ Override
202
209
public void onPlayFromMediaId (String data , Bundle extras ) {
210
+ Log .d (TAG , "------------------------------------------onPlayFromMediaId-------------------------------------------" );
203
211
Log .d (TAG , "onPlayFromMediaId" );
204
212
super .onPlayFromMediaId (data , extras );
205
213
@@ -215,7 +223,7 @@ public void onPlayFromMediaId(String data, Bundle extras) {
215
223
//Should be started but sometimes not :s
216
224
if (!isServiceStarted ){
217
225
Log .d (TAG , "onPlayFromMediaId: Starting Service" );
218
- startService (new Intent (getApplicationContext (), MusicPlayerService .class ));
226
+ startForegroundService (new Intent (getApplicationContext (), MusicPlayerService .class ));
219
227
}
220
228
221
229
Log .d (TAG , "onPlayFromMediaId: data is " + data );
@@ -235,16 +243,20 @@ public void onPlayFromMediaId(String data, Bundle extras) {
235
243
// //afd.close();
236
244
// initMediaSessionMetadata();
237
245
// Notify fragments
238
- mLocalBroadcastManager .sendBroadcast (new Intent ("UPDATE" ));
246
+ // mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
239
247
}
240
248
241
249
@ Override
242
250
public void onCommand (String command , Bundle extras , ResultReceiver cb ) {
251
+ Log .d (TAG , "------------------------------------------onCommand-------------------------------------------" );
243
252
super .onCommand (command , extras , cb );
244
253
// if( COMMAND_EXAMPLE.equalsIgnoreCase(command) ) {
245
254
// //Custom command here
246
255
// }
256
+
247
257
mLocalBroadcastManager .sendBroadcast (new Intent ("UPDATE" ));
258
+ currentNotification = buildNotification ();
259
+ startForeground (NOTIFICATION_ID , currentNotification );
248
260
}
249
261
250
262
@ Override
@@ -254,6 +266,7 @@ public void onSeekTo(long pos) {
254
266
255
267
@ Override
256
268
public void onStop () {
269
+ Log .d (TAG , "------------------------------------------on STOP-------------------------------------------" );
257
270
Log .d (TAG , "onStop called" );
258
271
releaseAudioFocus ();
259
272
setMediaPlaybackState (PlaybackStateCompat .STATE_STOPPED );
@@ -266,13 +279,14 @@ public void onStop() {
266
279
stopForeground (true );
267
280
268
281
// Notify fragments
269
- mLocalBroadcastManager .sendBroadcast (new Intent ("UPDATE" ));
282
+ // mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
270
283
super .onStop ();
271
284
272
285
}
273
286
274
287
@ Override
275
288
public void onSkipToNext () {
289
+ Log .d (TAG , "------------------------------------------onSkipToNext-------------------------------------------" );
276
290
Song nextSong = mMusicLibrary .getNextSong (); // TODO GET NEXT SONG
277
291
if (null != nextSong ) {
278
292
Log .d (TAG , "onSkipToNext: nextSong is '" + nextSong .getTitle () + "'" );
@@ -285,12 +299,13 @@ public void onSkipToNext() {
285
299
}
286
300
287
301
// Notify fragments
288
- mLocalBroadcastManager .sendBroadcast (new Intent ("UPDATE" ));
302
+ // mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
289
303
super .onSkipToNext ();
290
304
}
291
305
292
306
@ Override
293
307
public void onSkipToPrevious () {
308
+ Log .d (TAG , "------------------------------------------onSkipToPrevious-------------------------------------------" );
294
309
Song prevSong = mMusicLibrary .getPreviousSong (); // TODO GET PREV SONG
295
310
if (null != prevSong ) {
296
311
Log .d (TAG , "onSkipToPrevious: prevSong is '" + prevSong .getTitle () + "'" );
@@ -302,7 +317,7 @@ public void onSkipToPrevious() {
302
317
}
303
318
304
319
// Notify fragments
305
- mLocalBroadcastManager .sendBroadcast (new Intent ("UPDATE" ));
320
+ // mLocalBroadcastManager.sendBroadcast(new Intent("UPDATE"));
306
321
super .onSkipToPrevious ();
307
322
}
308
323
@@ -311,6 +326,7 @@ public void onSkipToPrevious() {
311
326
312
327
@ Override
313
328
public void onCreate () {
329
+ Log .d (TAG , "------------------------------------------onCreate-------------------------------------------" );
314
330
super .onCreate ();
315
331
316
332
// --------------------------External Communications----------------------
@@ -370,25 +386,27 @@ public void onCreate() {
370
386
371
387
// Shite fix for shite issue
372
388
//https://stackoverflow.com/questions/44425584/context-startforegroundservice-did-not-then-call-service-startforeground
373
- if (Build .VERSION .SDK_INT >= 26 ) {
374
- NotificationChannel channel = new NotificationChannel (CHANNEL_ID ,
375
- CHANNEL_ID ,
376
- NotificationManager .IMPORTANCE_MIN );
377
-
378
- ((NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE )).createNotificationChannel (channel );
379
-
380
- Notification notification = new NotificationCompat .Builder (this , CHANNEL_ID )
381
- .setContentTitle ("" )
382
- .setContentText ("" ).build ();
383
-
384
- startForeground (1 , notification );
385
- }
389
+ // if (Build.VERSION.SDK_INT >= 26) {
390
+ // NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
391
+ // CHANNEL_ID,
392
+ // NotificationManager.IMPORTANCE_MIN);
393
+ //
394
+ // ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
395
+ //
396
+ // Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
397
+ // .setContentTitle("")
398
+ // .setContentText("").build();
399
+ //
400
+ // startForeground(1, notification);
401
+ // }
386
402
387
403
Log .d (TAG , "onCreate MusicService creating MediaSession, MediaPlayer, and MediaNotificationManager" );
388
404
}
389
405
390
406
@ Override
391
407
public void onDestroy () {
408
+ Log .d (TAG , "------------------------------------------onDestroy-------------------------------------------" );
409
+
392
410
Log .d (TAG , "onDestroy" );
393
411
releaseAudioFocus ();
394
412
removeNoisyReceiver ();
@@ -397,16 +415,16 @@ public void onDestroy() {
397
415
mMediaPlayer .reset ();
398
416
Log .d (TAG , "onDestroy: mediaplayer.release()" );
399
417
mMediaPlayer .release ();
400
- mMediaPlayer = null ;
418
+ // mMediaPlayer = null;
401
419
}
402
420
if (mMediaSession != null ) {
403
421
Log .d (TAG , "onDestroy: mediasession.release()" );
404
422
mMediaSession .release ();
405
- mMediaSession = null ;
423
+ // mMediaSession = null;
406
424
}
407
425
408
- Log .d (TAG , "onDestroy: stop Foreground" );
409
- stopForeground (true );
426
+ // Log.d(TAG, "onDestroy: stop Foreground");
427
+ // stopForeground(true);
410
428
411
429
412
430
//Log.d(TAG, "onDestroy: cancelInitSongs");
@@ -420,6 +438,7 @@ public void onDestroy() {
420
438
421
439
@ Override
422
440
public void onTaskRemoved (Intent rootIntent ) {
441
+ Log .d (TAG , "------------------------------------------on TASK REMVOED-------------------------------------------" );
423
442
Log .d (TAG , "onTaskRemoved" );
424
443
425
444
// TODO if for some reason the app is closed before the library is able to load
@@ -437,6 +456,7 @@ public void onTaskRemoved(Intent rootIntent) {
437
456
}
438
457
439
458
private void initMediaController () {
459
+ Log .d (TAG , "------------------------------------------initMediaController-------------------------------------------" );
440
460
try {
441
461
mMediaController = new MediaControllerCompat (getApplicationContext (), mMediaSession .getSessionToken ());
442
462
} catch (Exception e ) {
@@ -449,13 +469,15 @@ private void initMediaController() {
449
469
private static boolean isNoisyReceiverActive = false ;
450
470
451
471
private void initNoisyReceiver () {
472
+ Log .d (TAG , "------------------------------------------initNoisyReceiver-------------------------------------------" );
452
473
//Handles headphones coming unplugged. cannot be done through a manifest receiver
453
474
IntentFilter filter = new IntentFilter (AudioManager .ACTION_AUDIO_BECOMING_NOISY );
454
475
registerReceiver (mNoisyReceiver , filter );
455
476
isNoisyReceiverActive = true ;
456
477
}
457
478
458
479
private void removeNoisyReceiver (){
480
+ Log .d (TAG , "------------------------------------------removeNoisyReceiver-------------------------------------------" );
459
481
if (isNoisyReceiverActive ){
460
482
Log .d (TAG , "removeNoisyReceiver: removing" );
461
483
unregisterReceiver (mNoisyReceiver );
@@ -467,6 +489,7 @@ private void removeNoisyReceiver(){
467
489
}
468
490
469
491
private void initMediaPlayer () {
492
+ Log .d (TAG , "------------------------------------------initMediaPlayer-------------------------------------------" );
470
493
mAudioManager = (mAudioManager == null ) ? (AudioManager ) getApplicationContext ().getSystemService (Context .AUDIO_SERVICE ) : mAudioManager ;
471
494
if ( mMediaPlayer == null ) {
472
495
mMediaPlayer = new MediaPlayer ();
@@ -483,6 +506,7 @@ private void initMediaPlayer() {
483
506
}
484
507
485
508
private void initMediaSession () {
509
+ Log .d (TAG , "------------------------------------------initMediaSession-------------------------------------------" );
486
510
ComponentName mediaButtonReceiver = new ComponentName (getApplicationContext (), MediaButtonReceiver .class );
487
511
// Create a MediaSession
488
512
mMediaSession = new MediaSessionCompat (getApplicationContext (), TAG );
@@ -525,23 +549,8 @@ private void setMediaPlaybackState(int state) {
525
549
mMediaSession .setPlaybackState (playbackStateBuilder .build ());
526
550
}
527
551
528
- private void initMediaSessionMetadata () {
529
- MediaMetadataCompat .Builder metadataBuilder = new MediaMetadataCompat .Builder ();
530
- //Notification icon in card
531
- metadataBuilder .putBitmap (MediaMetadata .METADATA_KEY_DISPLAY_ICON , BitmapFactory .decodeResource (getResources (), R .mipmap .ic_launcher ));
532
- metadataBuilder .putBitmap (MediaMetadata .METADATA_KEY_ALBUM_ART , BitmapFactory .decodeResource (getResources (), R .mipmap .ic_launcher ));
533
-
534
- //lock screen icon for pre lollipop
535
- metadataBuilder .putBitmap (MediaMetadata .METADATA_KEY_ART , BitmapFactory .decodeResource (getResources (), R .mipmap .ic_launcher ));
536
- metadataBuilder .putString (MediaMetadata .METADATA_KEY_DISPLAY_TITLE , "Display Title" );
537
- metadataBuilder .putString (MediaMetadata .METADATA_KEY_DISPLAY_SUBTITLE , "Display Subtitle" );
538
- metadataBuilder .putLong (MediaMetadata .METADATA_KEY_TRACK_NUMBER , 1 );
539
- metadataBuilder .putLong (MediaMetadata .METADATA_KEY_NUM_TRACKS , 1 );
540
-
541
- mMediaSession .setMetadata (metadataBuilder .build ());
542
- }
543
-
544
552
private boolean getAudioFocus () {
553
+ Log .d (TAG , "------------------------------------------getAudioFocus-------------------------------------------" );
545
554
Log .d (TAG , "tryToGetAudioFocus" );
546
555
int result = mAudioManager .requestAudioFocus (mAudioFocusRequest );
547
556
if (result == AudioManager .AUDIOFOCUS_REQUEST_GRANTED ) {
@@ -556,6 +565,7 @@ private boolean getAudioFocus() {
556
565
}
557
566
558
567
private void releaseAudioFocus () {
568
+ Log .d (TAG , "------------------------------------------releaseAudioFocus-------------------------------------------" );
559
569
Log .d (TAG , "giveUpAudioFocus" );
560
570
if (mAudioManager .abandonAudioFocusRequest (mAudioFocusRequest )
561
571
== AudioManager .AUDIOFOCUS_REQUEST_GRANTED ) {
@@ -569,6 +579,7 @@ private void releaseAudioFocus() {
569
579
570
580
@ Override
571
581
public void onAudioFocusChange (int focusChange ) {
582
+ Log .d (TAG , "------------------------------------------onAudioFocusChange-------------------------------------------" );
572
583
Log .d (TAG , "onAudioFocusChange. focusChange: " + focusChange );
573
584
switch (focusChange ) {
574
585
case AudioManager .AUDIOFOCUS_GAIN :
@@ -634,6 +645,7 @@ public void onCompletion(MediaPlayer mediaPlayer) {
634
645
635
646
@ Override
636
647
public int onStartCommand (Intent intent , int flags , int startId ) {
648
+ Log .d (TAG , "------------------------------------------onStartCommand-------------------------------------------" );
637
649
Log .d (TAG , "onStartCommand" );
638
650
isServiceStarted = true ;
639
651
// TODO USE APP COMPAT EVERYTHING
@@ -645,6 +657,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
645
657
646
658
@ Override
647
659
public void onPrepared (MediaPlayer mediaPlayer ) {
660
+ Log .d (TAG , "------------------------------------------onPrepared-------------------------------------------" );
648
661
Log .d (TAG , "onPrepared" );
649
662
mMediaSession .setActive (true );
650
663
mediaPlayer .start ();
@@ -656,7 +669,7 @@ public void onPrepared(MediaPlayer mediaPlayer) {
656
669
ContextCompat .startForegroundService (this , intent );
657
670
658
671
currentNotification = buildNotification ();
659
- this . startForeground (NOTIFICATION_ID , currentNotification );
672
+ startForeground (NOTIFICATION_ID , currentNotification );
660
673
}
661
674
662
675
// ------------------------------Notifications--------------------------------
@@ -665,6 +678,8 @@ public void onPrepared(MediaPlayer mediaPlayer) {
665
678
666
679
private Notification buildNotification (){
667
680
681
+ Log .d (TAG , "------------------------------------------NOTIFICATION-------------------------------------------" );
682
+
668
683
Log .d (TAG , "Creating notification" );
669
684
670
685
// Get current song
@@ -706,7 +721,8 @@ private Notification buildNotification(){
706
721
.setContentIntent (createContentIntent (current_song ))
707
722
.setColor (ContextCompat .getColor (this , R .color .colorAccent ))
708
723
.setLargeIcon (albumArt )
709
- .setSmallIcon ((mPlaybackState .getState () == PlaybackStateCompat .STATE_PLAYING ) ? android .R .drawable .ic_media_pause : android .R .drawable .ic_media_play )
724
+ //.setSmallIcon((mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play)
725
+ .setSmallIcon (R .drawable .icons8musicalnotes24 )
710
726
// Title - Usually Song name.
711
727
.setContentTitle (current_song .getTitle ())
712
728
// Subtitle - Usually Artist name.
@@ -751,19 +767,21 @@ private Notification buildNotification(){
751
767
.setMediaSession (mMediaSession .getSessionToken ())
752
768
.setShowActionsInCompactView (0 , 1 , 2 ));
753
769
754
- if (null == mPlaybackState ){
755
- Log .d (TAG , "null == playbackState OR !notificationStarted" );
756
- // "Die die die"
757
- this .stopForeground (true );
758
- }
770
+ // if (null == mPlaybackState){
771
+ // Log.d(TAG, "null == playbackState OR !notificationStarted");
772
+ // // "Die die die"
773
+ // this.stopForeground(true);
774
+ // }
759
775
760
776
Log .d (TAG , "createNotificationChannel: build" );
761
777
778
+ Log .d (TAG , "-----------------------------------NOTIFICATION BUILT-------------------------------------------" );
762
779
return notificationBuilder .build ();
763
780
}
764
781
765
782
// Set the notification's tap action
766
783
private PendingIntent createContentIntent (Song song ){
784
+ Log .d (TAG , "------------------------------------------createContentIntent-------------------------------------------" );
767
785
Intent openUI = new Intent (this , MainActivity .class );
768
786
// IF encountering problems return to the stack overflow post:
769
787
// https://stackoverflow.com/questions/29321261/what-are-the-differences-between-flag-activity-reset-task-if-needed-and-flag-act
0 commit comments