I've been looking everywhere about how to call a function in background (from main isolated process / foreground), but there's no result so far.
This is roughly the process I'm talking about:
import 'package:audioplayers/audioplayers.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(myBackgroundMessageHandler);
runApp(MyApp());
}
Future<void> myBackgroundMessageHandler(RemoteMessage message) async {
flutterLocalNotificationsPlugin.show(
0,
"Title",
"Body",
NotificationDetails(
android: AndroidNotificationDetails(
"channelid",
"none",
"none",
priority: Priority.high,
),
)
);
audioPlayer = await audioPlayerCache.loop("ring.mp3", isNotification: true);
}
void stopRing() async {
await audioPlayer.stop();
}
When the audioPlayer is called in background, calling stopRing() function from foreground will NOT affect anything because - I think - they are in different isolated process.
Here is the flow: app receive firebase notification (in background) -> show notification (using flutter_local_notifications plugin) and play sound -> user tap the notification -> wakeup the app -> call stopRing() (not working, the app is already in foreground).
So, my question is, how to call stopRing() in background from foreground process, or probably just simply terminate the background process, I guess that will stop the ring, too.
Thanks in advance...
(Note: everything else is working well, except the problem mentioned in this post)