Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Update to onError to avoid crash. #101

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -81,24 +78,85 @@ 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) {
Log.i(TAG, "onGetRoot");
return new BrowserRoot(MEDIA_ROOT_ID, null);
}


@Override
public void onLoadChildren(
final String parentMediaId, final Result<List<MediaBrowserCompat.MediaItem>> result) {
Log.i(TAG, "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)) {
Expand All @@ -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() {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -284,7 +372,6 @@ private Notification buildNotification() {
.setStyle(
new MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(compactNotificationActionIndices)
.setShowCancelButton(true)
.setCancelButtonIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 3 additions & 2 deletions packages/audiofileplayer/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down