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 967e483..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 @@ -57,18 +57,15 @@ public interface ServiceListener { void onSeekTo(long positionMs); } + @Override 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 - ); - mediaSession = new MediaSessionCompat(this, TAG, null, pendingItent); + // 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 | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); @@ -81,7 +78,57 @@ public void onCreate() { 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(); + + + /* 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") + .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, + "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 public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints) { @@ -89,6 +136,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<List<MediaBrowserCompat.MediaItem>> result) { @@ -96,9 +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(); + // 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)) { @@ -114,6 +172,33 @@ public int onStartCommand(final Intent intent, int flags, int startId) { } 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() { @@ -126,8 +211,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); } @@ -213,7 +301,7 @@ public void setActions( private int getSmallIconId() { Context context = getApplicationContext(); - String iconUri = "mipmap/ic_launcher"; + String iconUri = "mipmap/notilogo72x72"; try { ApplicationInfo ai = @@ -284,7 +372,6 @@ private Notification buildNotification() { .setStyle( new MediaStyle() .setMediaSession(mediaSession.getSessionToken()) - .setShowActionsInCompactView(compactNotificationActionIndices) .setShowCancelButton(true) .setCancelButtonIntent( MediaButtonReceiver.buildMediaButtonPendingIntent( @@ -317,8 +404,10 @@ private void createChannel() { public class MediaSessionCallback extends MediaSessionCompat.Callback { @Override + public void onPlay() { Log.i(TAG, "MediaSessionCallback.onPlay"); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService( new Intent(AudiofileplayerService.this, AudiofileplayerService.class)); @@ -327,13 +416,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 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); } } diff --git a/packages/audiofileplayer/pubspec.yaml b/packages/audiofileplayer/pubspec.yaml index b685f9f..1071e96 100644 --- a/packages/audiofileplayer/pubspec.yaml +++ b/packages/audiofileplayer/pubspec.yaml @@ -1,7 +1,8 @@ name: audiofileplayer description: A Flutter plugin for audio playback. -version: 2.0.1 -homepage: https://github.com/google/flutter.plugins/tree/master/packages/audiofileplayer +version: 2.1.3 +homepage: https://github.com/arodriguezgb/flutter.plugins/tree/master/packages/audiofileplayer + environment: sdk: '>=2.12.0 <3.0.0'