From c14b50b325bcc88cd3e43f3da1ab4af42a6511ad Mon Sep 17 00:00:00 2001 From: arodriguezgb <53869855+arodriguezgb@users.noreply.github.com> Date: Sun, 31 Jan 2021 01:27:20 -0400 Subject: [PATCH 01/14] Update AudiofileplayerService.java --- .../AudiofileplayerService.java | 91 +++++++++++++++---- 1 file changed, 71 insertions(+), 20 deletions(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 3972bd4..55a791b 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -73,8 +73,27 @@ public void onCreate() { mediaSessionCallback = new MediaSessionCallback(); // Do i need this as ivar? mediaSession.setCallback(mediaSessionCallback); - setSessionToken(mediaSession.getSessionToken()); + + Notification notif = buildNotification(); + + + // TESTING IF THIS HELPS WITH THE CRASHES & ANRS + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // Display the notification and place the service in the foreground + startForeground(NOTIFICATION_ID, notif); + + } +/* if (Build.VERSION.SDK_INT >= 26) { + String CHANNEL_ID = "GentleBirth"; + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, + "GentleBirth", NotificationManager.IMPORTANCE_DEFAULT); + ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel); + Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) + .setContentTitle("") + .setContentText("").build(); + startForeground(1, notification); + }*/ } @Override @@ -83,6 +102,7 @@ public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle roo return new BrowserRoot(MEDIA_ROOT_ID, null); } + @Override public void onLoadChildren( final String parentMediaId, final Result> result) { @@ -93,16 +113,21 @@ public void onLoadChildren( @Override public int onStartCommand(final Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand"); - if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()) - && intent.hasExtra(AudiofileplayerPlugin.CUSTOM_MEDIA_BUTTON_EXTRA_KEY)) { - // Check for custom button intent. - handleCustomButtonIntent(intent); - } else { - // If there is a KeyEvent in the intent, send it to the MediaButtonReceiver to pass to - // its callbacks. - MediaButtonReceiver.handleIntent(mediaSession, intent); + + if (intent != null){ + if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()) + && intent.hasExtra(AudiofileplayerPlugin.CUSTOM_MEDIA_BUTTON_EXTRA_KEY)) { + // Check for custom button intent. + handleCustomButtonIntent(intent); + } else { + // If there is a KeyEvent in the intent, send it to the MediaButtonReceiver to pass to + // its callbacks. + if (mediaSession != null){ + MediaButtonReceiver.handleIntent(mediaSession, intent); + } + } } - return super.onStartCommand(intent, flags, startId); + return START_NOT_STICKY; } @Override @@ -116,8 +141,11 @@ public void onDestroy() { @Override public void onTaskRemoved(Intent rootIntent) { Log.i(TAG, "onTaskRemoved"); - stopForeground(true); - stopSelf(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + stopForeground(true); + } else { + stopSelf(); + } super.onTaskRemoved(rootIntent); } @@ -144,7 +172,9 @@ private void handleCustomButtonIntent(Intent intent) { String eventId = (String) intent.getExtras().get(AudiofileplayerPlugin.CUSTOM_MEDIA_BUTTON_EXTRA_KEY); Log.d(TAG, "Got custom button intent with eventId:" + eventId); - listener.onCustomMediaButtonClick(eventId); + if (listener != null){ + listener.onCustomMediaButtonClick(eventId); + } } public void stop() { @@ -270,8 +300,9 @@ private Notification buildNotification() { // Set the media style to show icons for the Actions. .setStyle( new MediaStyle() - .setMediaSession(mediaSession.getSessionToken()) - .setShowActionsInCompactView(compactNotificationActionIndices) + .setMediaSession(mediaSession.getSessionToken() + // Commenting this so that there is no action when COMPACT to avoid some crashes that were happening. + // .setShowActionsInCompactView(compactNotificationActionIndices) .setShowCancelButton(true) .setCancelButtonIntent( MediaButtonReceiver.buildMediaButtonPendingIntent( @@ -304,13 +335,29 @@ private void createChannel() { public class MediaSessionCallback extends MediaSessionCompat.Callback { @Override + public void onPlay() { Log.i(TAG, "MediaSessionCallback.onPlay"); + Notification notif = buildNotification(); + startService(new Intent(AudiofileplayerService.this, AudiofileplayerService.class)); + + // TESTING IF THIS HELPS WITH THE CRASHES & ANRS + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + + startForegroundService(new Intent(AudiofileplayerService.this, AudiofileplayerService.class)); + + // Display the notification and place the service in the foreground + startForeground(NOTIFICATION_ID, notif); + + } else { + // print("running startService"); + startService(new Intent(AudiofileplayerService.this, AudiofileplayerService.class)); + + } + if (!mediaSession.isActive()) mediaSession.setActive(true); - Notification notif = buildNotification(); - // Display the notification and place the service in the foreground - startForeground(NOTIFICATION_ID, notif); + } @Override @@ -337,7 +384,9 @@ public boolean onMediaButtonEvent(Intent mediaButtonEvent) { final KeyEvent event = (KeyEvent) mediaButtonEvent.getExtras().get(Intent.EXTRA_KEY_EVENT); if (event.getAction() == KeyEvent.ACTION_DOWN) { Log.i(TAG, "event key code:" + event.getKeyCode()); - listener.onMediaButtonClick(event.getKeyCode()); + if (listener != null) { + listener.onMediaButtonClick(event.getKeyCode()); + } } return true; @@ -346,7 +395,9 @@ public boolean onMediaButtonEvent(Intent mediaButtonEvent) { @Override public void onSeekTo(long positionMs) { Log.i(TAG, "MediaSessionCallback.onSeekTo:" + positionMs); - listener.onSeekTo(positionMs); + if (listener != null) { + listener.onSeekTo(positionMs); + } } } From 3d79353a6f97a31b2fd567e0fb36bbd0042c4d6e Mon Sep 17 00:00:00 2001 From: arodriguezgb <53869855+arodriguezgb@users.noreply.github.com> Date: Wed, 3 Feb 2021 12:12:44 -0400 Subject: [PATCH 02/14] Update AudiofileplayerService.java --- .../AudiofileplayerService.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 55a791b..e9c9f07 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -56,6 +56,7 @@ public interface ServiceListener { void onSeekTo(long positionMs); } + @Override public void onCreate() { super.onCreate(); @@ -81,8 +82,17 @@ public void onCreate() { // TESTING IF THIS HELPS WITH THE CRASHES & ANRS if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Display the notification and place the service in the foreground - startForeground(NOTIFICATION_ID, notif); + //startForeground(NOTIFICATION_ID, notif); + String CHANNEL_ID = "GentleBirth"; + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, + "GentleBirth", NotificationManager.IMPORTANCE_NONE); + ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel); + Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) + .setSmallIcon(getSmallIconId()) + .setContentTitle("The app is running in the background") + .setContentText("Swipe for more information or to stop the app.").build(); + startForeground(11241223, notification); } /* if (Build.VERSION.SDK_INT >= 26) { String CHANNEL_ID = "GentleBirth"; @@ -230,7 +240,7 @@ public void setActions( private int getSmallIconId() { Context context = getApplicationContext(); - String iconUri = "mipmap/ic_launcher"; + String iconUri = "mipmap/notilogo72x72"; try { ApplicationInfo ai = @@ -300,9 +310,7 @@ private Notification buildNotification() { // Set the media style to show icons for the Actions. .setStyle( new MediaStyle() - .setMediaSession(mediaSession.getSessionToken() - // Commenting this so that there is no action when COMPACT to avoid some crashes that were happening. - // .setShowActionsInCompactView(compactNotificationActionIndices) + .setMediaSession(mediaSession.getSessionToken()) .setShowCancelButton(true) .setCancelButtonIntent( MediaButtonReceiver.buildMediaButtonPendingIntent( From b82d6b11a650daac8a911f3d79d17ad129801518 Mon Sep 17 00:00:00 2001 From: arodriguezgb <53869855+arodriguezgb@users.noreply.github.com> Date: Mon, 8 Feb 2021 15:28:03 -0400 Subject: [PATCH 03/14] Update AudiofileplayerService.java --- .../AudiofileplayerService.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 6859878..9fb17da 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -62,6 +62,7 @@ public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate"); instance = this; + Notification notif = buildNotification(); mediaSession = new MediaSessionCompat(this, TAG); mediaSession.setFlags( @@ -76,7 +77,23 @@ public void onCreate() { mediaSession.setCallback(mediaSessionCallback); setSessionToken(mediaSession.getSessionToken()); - } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // Display the notification and place the service in the foreground + //startForeground(NOTIFICATION_ID, notif); + String CHANNEL_ID = "GentleBirth"; + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, + "GentleBirth", NotificationManager.IMPORTANCE_NONE); + ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel); + Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) + .setSmallIcon(getSmallIconId()) + .setContentTitle("The app is running in the background") + .setContentText("Swipe for more information or to stop the app.").build(); + + startForeground(11241223, notification); + } + + } @Override public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints) { From 4622dede1e4a71c6d83faaf28b7943ef5539241c Mon Sep 17 00:00:00 2001 From: arodriguezgb <53869855+arodriguezgb@users.noreply.github.com> Date: Mon, 8 Feb 2021 15:29:34 -0400 Subject: [PATCH 04/14] Update RemoteManagedMediaPlayer.java --- .../audiofileplayer/RemoteManagedMediaPlayer.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/RemoteManagedMediaPlayer.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/RemoteManagedMediaPlayer.java index 2529793..68fba86 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/RemoteManagedMediaPlayer.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/RemoteManagedMediaPlayer.java @@ -111,9 +111,12 @@ public void pause() { } } - @Override - public boolean onError(MediaPlayer mp, int what, int extra) { - onRemoteLoadListener.onRemoteLoadComplete(false); - return super.onError(mp, what, extra); + @Override public boolean onError(MediaPlayer mp, int what, int extra) { + try { + onRemoteLoadListener.onRemoteLoadComplete(false); + } catch(Exception e) { + + } + return super.onError(mp, what, extra); } } From 261093f4cc31bfa1b03f16ab77a935506602d863 Mon Sep 17 00:00:00 2001 From: arodriguezgb <53869855+arodriguezgb@users.noreply.github.com> Date: Wed, 10 Feb 2021 11:01:26 -0400 Subject: [PATCH 05/14] Update AudiofileplayerService.java --- .../audiofileplayer/AudiofileplayerService.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 9fb17da..59be096 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -62,7 +62,6 @@ public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate"); instance = this; - Notification notif = buildNotification(); mediaSession = new MediaSessionCompat(this, TAG); mediaSession.setFlags( @@ -78,21 +77,6 @@ public void onCreate() { setSessionToken(mediaSession.getSessionToken()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // Display the notification and place the service in the foreground - //startForeground(NOTIFICATION_ID, notif); - String CHANNEL_ID = "GentleBirth"; - NotificationChannel channel = new NotificationChannel(CHANNEL_ID, - "GentleBirth", NotificationManager.IMPORTANCE_NONE); - ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel); - Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) - .setSmallIcon(getSmallIconId()) - .setContentTitle("The app is running in the background") - .setContentText("Swipe for more information or to stop the app.").build(); - - startForeground(11241223, notification); - } - } @Override From 720503b6ba45f0a457e9f1be8415d284e5d533fa Mon Sep 17 00:00:00 2001 From: arodriguezgb <53869855+arodriguezgb@users.noreply.github.com> Date: Wed, 10 Feb 2021 11:02:04 -0400 Subject: [PATCH 06/14] Update AudiofileplayerService.java --- .../flutter/plugins/audiofileplayer/AudiofileplayerService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 59be096..c30ea44 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -76,7 +76,6 @@ public void onCreate() { mediaSession.setCallback(mediaSessionCallback); setSessionToken(mediaSession.getSessionToken()); - } @Override From 31071ba87c28646e5ad54385163c3e0451224b89 Mon Sep 17 00:00:00 2001 From: arodriguezgb <53869855+arodriguezgb@users.noreply.github.com> Date: Sun, 14 Feb 2021 15:06:05 -0400 Subject: [PATCH 07/14] Update pubspec.yaml --- packages/audiofileplayer/pubspec.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/audiofileplayer/pubspec.yaml b/packages/audiofileplayer/pubspec.yaml index 5540ce7..0e3ffa9 100644 --- a/packages/audiofileplayer/pubspec.yaml +++ b/packages/audiofileplayer/pubspec.yaml @@ -1,7 +1,7 @@ name: audiofileplayer description: A Flutter plugin for audio playback. -version: 1.3.3 -homepage: https://github.com/google/flutter.plugins/tree/master/packages/audiofileplayer +version: 1.4.0 +homepage: https://github.com/arodriguezgb/flutter.plugins/tree/master/packages/audiofileplayer environment: sdk: ">=2.2.0 <3.0.0" From 8021de0a299a43463c1c8a7ea7c7a86023bbe61b Mon Sep 17 00:00:00 2001 From: arodriguezgb <53869855+arodriguezgb@users.noreply.github.com> Date: Sat, 19 Jun 2021 18:40:35 -0400 Subject: [PATCH 08/14] Update AudiofileplayerService.java --- .../plugins/audiofileplayer/AudiofileplayerService.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 182eaa8..0e31636 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -80,8 +80,7 @@ public void onCreate() { Notification notif = buildNotification(); - // TESTING IF THIS HELPS WITH THE CRASHES & ANRS - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Display the notification and place the service in the foreground //startForeground(NOTIFICATION_ID, notif); String CHANNEL_ID = "GentleBirth"; @@ -90,8 +89,8 @@ public void onCreate() { ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(getSmallIconId()) - .setContentTitle("The app is running in the background") - .setContentText("Swipe for more information or to stop the app.").build(); + .setContentTitle("The app is running") + .setContentText("This ensures a better experience for you with GentleBirth.").build(); startForeground(11241223, notification); } From 7f28aef6e260801585766cde572177aa069eb0aa Mon Sep 17 00:00:00 2001 From: arodriguezgb <53869855+arodriguezgb@users.noreply.github.com> Date: Tue, 21 Sep 2021 19:24:48 -0400 Subject: [PATCH 09/14] Audio Player ANR FIX --- .../AudiofileplayerService.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 0e31636..d8a7cd1 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -77,10 +77,10 @@ public void onCreate() { mediaSession.setCallback(mediaSessionCallback); setSessionToken(mediaSession.getSessionToken()); - Notification notif = buildNotification(); + // Notification notif = buildNotification(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + /* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Display the notification and place the service in the foreground //startForeground(NOTIFICATION_ID, notif); String CHANNEL_ID = "GentleBirth"; @@ -93,7 +93,7 @@ public void onCreate() { .setContentText("This ensures a better experience for you with GentleBirth.").build(); startForeground(11241223, notification); - } + } */ /* if (Build.VERSION.SDK_INT >= 26) { String CHANNEL_ID = "GentleBirth"; NotificationChannel channel = new NotificationChannel(CHANNEL_ID, @@ -124,7 +124,14 @@ public void onLoadChildren( @Override public int onStartCommand(final Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand"); - + + Notification notif = buildNotification(); + // Display the notification and place the service in the foreground + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + startForeground(NOTIFICATION_ID, notif, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK); + } else { + startForeground(NOTIFICATION_ID, notif); + } if (intent != null) { if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()) && intent.hasExtra(AudiofileplayerPlugin.CUSTOM_MEDIA_BUTTON_EXTRA_KEY)) { @@ -357,13 +364,7 @@ public void onPlay() { } if (!mediaSession.isActive()) mediaSession.setActive(true); - Notification notif = buildNotification(); - // Display the notification and place the service in the foreground - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - startForeground(NOTIFICATION_ID, notif, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK); - } else { - startForeground(NOTIFICATION_ID, notif); - } + } @Override From 825938fece24cb8b2a5a1537eae904912d5e23b8 Mon Sep 17 00:00:00 2001 From: arodriguezgb <53869855+arodriguezgb@users.noreply.github.com> Date: Mon, 1 Nov 2021 22:51:45 -0400 Subject: [PATCH 10/14] Update pubspec.yaml --- packages/audiofileplayer/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/audiofileplayer/pubspec.yaml b/packages/audiofileplayer/pubspec.yaml index 279f988..c81a760 100644 --- a/packages/audiofileplayer/pubspec.yaml +++ b/packages/audiofileplayer/pubspec.yaml @@ -1,6 +1,6 @@ name: audiofileplayer description: A Flutter plugin for audio playback. -version: 2.1.0 +version: 2.1.1 homepage: https://github.com/arodriguezgb/flutter.plugins/tree/master/packages/audiofileplayer From 788ec81c19fe4c6fa5f8984eb4637b509cba1229 Mon Sep 17 00:00:00 2001 From: Arnaldo Rodriguez <53869855+arodriguezgb@users.noreply.github.com> Date: Thu, 20 Oct 2022 23:59:33 -0400 Subject: [PATCH 11/14] Update AudiofileplayerService.java --- .../flutter/plugins/audiofileplayer/AudiofileplayerService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 012ee7a..5a47f6c 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -69,7 +69,7 @@ public void onCreate() { PendingIntent.FLAG_IMMUTABLE ); - mediaSession = new MediaSessionCompat(this, TAG, null, pendingItent); + mediaSession = new MediaSessionCompat(this, TAG); mediaSession.setFlags( MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); From 050f1372fead16544b58d4724f38e7ed2182a794 Mon Sep 17 00:00:00 2001 From: Arnaldo Rodriguez <53869855+arodriguezgb@users.noreply.github.com> Date: Thu, 20 Oct 2022 23:59:51 -0400 Subject: [PATCH 12/14] Update pubspec.yaml --- packages/audiofileplayer/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/audiofileplayer/pubspec.yaml b/packages/audiofileplayer/pubspec.yaml index c81a760..062bd58 100644 --- a/packages/audiofileplayer/pubspec.yaml +++ b/packages/audiofileplayer/pubspec.yaml @@ -1,6 +1,6 @@ name: audiofileplayer description: A Flutter plugin for audio playback. -version: 2.1.1 +version: 2.1.2 homepage: https://github.com/arodriguezgb/flutter.plugins/tree/master/packages/audiofileplayer From 77d1787353bf790834a72dd69e15f26938c81f7a Mon Sep 17 00:00:00 2001 From: Arnaldo Rodriguez <53869855+arodriguezgb@users.noreply.github.com> Date: Thu, 18 Jan 2024 19:29:23 -0400 Subject: [PATCH 13/14] Update AudiofileplayerService.java --- .../AudiofileplayerService.java | 65 ++++++++++++++++--- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 5a47f6c..5c91477 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -63,12 +63,8 @@ public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate"); instance = this; - PendingIntent pendingItent = PendingIntent.getBroadcast( - getApplicationContext(), - 0, new Intent(Intent.ACTION_MEDIA_BUTTON), - PendingIntent.FLAG_IMMUTABLE - ); + // TODO: Migrate to MediaSession, re-add support for hardware buttons via a BroadcastService. mediaSession = new MediaSessionCompat(this, TAG); mediaSession.setFlags( MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS @@ -80,7 +76,29 @@ public void onCreate() { mediaSessionCallback = new MediaSessionCallback(); // Do i need this as ivar? mediaSession.setCallback(mediaSessionCallback); + setSessionToken(mediaSession.getSessionToken()); + + // Log.i(TAG, "onCreate"); + // instance = this; + // // PendingIntent pendingItent = PendingIntent.getBroadcast( + // // getApplicationContext(), + // // 0, new Intent(Intent.ACTION_MEDIA_BUTTON), + // // PendingIntent.FLAG_IMMUTABLE + // // ); + + // mediaSession = new MediaSessionCompat(this, TAG); + // mediaSession.setFlags( + // MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS + // | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); + // PlaybackStateCompat.Builder stateBuilder = + // new PlaybackStateCompat.Builder() + // .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE); + // mediaSession.setPlaybackState(stateBuilder.build()); + + // mediaSessionCallback = new MediaSessionCallback(); // Do i need this as ivar? + // mediaSession.setCallback(mediaSessionCallback); + // setSessionToken(mediaSession.getSessionToken()); // Notification notif = buildNotification(); @@ -126,17 +144,19 @@ public void onLoadChildren( result.sendResult(null); } - @Override + @Override public int onStartCommand(final Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand"); - - Notification notif = buildNotification(); + + + Notification notif = buildNotification(); // Display the notification and place the service in the foreground if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { startForeground(NOTIFICATION_ID, notif, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK); } else { startForeground(NOTIFICATION_ID, notif); } + if (intent != null) { if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()) && intent.hasExtra(AudiofileplayerPlugin.CUSTOM_MEDIA_BUTTON_EXTRA_KEY)) { @@ -150,8 +170,35 @@ public int onStartCommand(final Intent intent, int flags, int startId) { } } } - return START_NOT_STICKY; + return super.onStartCommand(intent, flags, startId); } + + // @Override + // public int onStartCommand(final Intent intent, int flags, int startId) { + // Log.i(TAG, "onStartCommand"); + + // Notification notif = buildNotification(); + // // Display the notification and place the service in the foreground + // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + // startForeground(NOTIFICATION_ID, notif, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK); + // } else { + // startForeground(NOTIFICATION_ID, notif); + // } + // if (intent != null) { + // if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()) + // && intent.hasExtra(AudiofileplayerPlugin.CUSTOM_MEDIA_BUTTON_EXTRA_KEY)) { + // // Check for custom button intent. + // handleCustomButtonIntent(intent); + // } else { + // // If there is a KeyEvent in the intent, send it to the MediaButtonReceiver to pass to + // // its callbacks. + // if (mediaSession != null) { + // MediaButtonReceiver.handleIntent(mediaSession, intent); + // } + // } + // } + // return START_NOT_STICKY; + // } @Override public void onDestroy() { From 2b578e82207dc2ab3dbd591b18d19ab4f7ea9f65 Mon Sep 17 00:00:00 2001 From: Arnaldo Rodriguez <53869855+arodriguezgb@users.noreply.github.com> Date: Thu, 18 Jan 2024 19:29:45 -0400 Subject: [PATCH 14/14] Update pubspec.yaml --- packages/audiofileplayer/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/audiofileplayer/pubspec.yaml b/packages/audiofileplayer/pubspec.yaml index 062bd58..1071e96 100644 --- a/packages/audiofileplayer/pubspec.yaml +++ b/packages/audiofileplayer/pubspec.yaml @@ -1,6 +1,6 @@ name: audiofileplayer description: A Flutter plugin for audio playback. -version: 2.1.2 +version: 2.1.3 homepage: https://github.com/arodriguezgb/flutter.plugins/tree/master/packages/audiofileplayer