I am using the flutter_notification_listener library to get info related to the currently playing song (on Spotify). Then, when the user presses a button, the song either pauses/resumes. This works perfectly! More than this I was curious to know if there was any method to change the progress of the song using a progress bar in the app? Like: when the user drags the progress bar the song's progress is also changed? This is the code I'm using now:
late NotificationEvent audioEvent;
void onData(NotificationEvent event) {
setState(() {
audioEvent = event;
});
}
Future<void> initPlatformState() async {
NotificationsListener.initialize();
NotificationsListener.receivePort?.listen((evt) => onData(evt));
}
void startListening() async {
var hasPermission = await NotificationsListener.hasPermission;
if (!hasPermission!) {
NotificationsListener.openPermissionSettings();
return;
}
var isR = await NotificationsListener.isRunning;
if (!isR!) {
await NotificationsListener.startService();
}
}
void playPause() {
audioEvent.actions?[2].tap(); // It works
}
@override
void initState() {
initPlatformState();
Future.delayed(const Duration(seconds: 2), () => startListening());
super.initState();
}
Button:
TextButton(onPressed: playPause, child: const Text('Play/Pause'))
Any help is highly appreciated ! Thank you
Finally got the solution. By default there is no library/plugin, that I know of, can achieve this. We have to take advantage of the platform channels in flutter and use native java.
I created a new MethodChannel and invoked a method to communicate with the java code:
Next follow the steps in the platform channels docs.
MainActivity.java :
MyNotificationService.java:
Hope it helps! (Thx to https://stackoverflow.com/a/66795488/14326852)