I want to play a ringtone, while my flutter app is in the background, and let the user stop it.
I want to play a ringtone after a specific interval that the user chooses (for example 3 minutes or 58 minutes), and then the user should be able to stop the ringtone. This works when the flutter app is in the foreground, but not when the app is in the background(For example when the screen is closed).
I'm currently using the callbackDispatcher fron the Workmanager plugin for flutter, which creates a new Isolate. Let's call it isolate B, to distinguish it from isolate A that is created when flutter starts my app.
Here is the callbackDispatcher:
void callbackDispatcher() {
print('callbackDispatcher');
FlutterRingtonePlayer.play(
android: AndroidSounds.ringtone,
ios: IosSounds.glass,
looping: false,
// Android only - API >= 28
volume: 1,
// Android only - API >= 28
asAlarm: true, // Android only - all APIs
);
return Future.value(true);
});
}
The problems is that I can start playing the ringtone from Isolate B, but I don't know how to let the user stop it.
So far I've tried:
- Calling FlutterRingtonePlayer.stop() from Isolate A when a notification is clicked, but that doesn't stop the ringtone that is playing in Isolate B.
- Starting the notification from Isolate B, and then stop the ringtone in Isolate B once clicked. But for some reason the notification doesn't popup when it's created in Isolate B.
- Calling FlutterRingtonePlayer.stop() from Isolate A, but that doesn't stop the ringtone that is playing in Isolate B.
- Making a global class with a method for playing and stopping ringtone and using it in Isolate B to play the ringtone and using it in Isolate A to stop the ringtone, but this also doesn't work...
I don't know how to communicate with Isolate B that Workmanager creates, and it seems like their documentation doesn't specify how to communicate with the new Isolate that is created...
What else can I try? My only goal is to play a Ringtone in background mode and let the user stop it.