How can I give notification icons optional functionality?
In addition to the normal function of the icons, I want to give other optional functions to the icons For example, after the next hit, another function is executed and ..
In fact, I want to listen to the changes of the main actions (next, previous, pause ..)
If you look at the official audio_service example, specifically example/lib/example_multiple_handlers.dart, you will find it defines the skipToQueueItem
method to do "normal functions" like seeking to the requested item:
@override
Future<void> skipToQueueItem(int index) async {
// Then default implementations of skipToNext and skipToPrevious provided by
// the [QueueHandler] mixin will delegate to this method.
if (index < 0 || index >= queue.value.length) return;
// This jumps to the beginning of the queue item at newIndex.
_player.seek(Duration.zero, index: index);
// Demonstrate custom events.
customEvent.add('skip to $index');
}
If you want to do custom things, just add your custom code at the end of this method. Note that in the example, the callbacks for skipToNext
and skipToPrevious
just delegate to the above method so that skipping to a track is handled in this one place above.
In fact, I want to listen to the changes of the main actions (next, previous, pause ..)
The approach is exactly the same. Just override each of these callbacks and handle each one to first do the "normal functions" followed by your custom code.
For example, you can override skipToNext
like this:
@override
Future<void> skipToNext() async {
// Do the normal functions
await _player.seekToNext();
// Add your custom code here
}
The approach is identical for every callback, so all you need to do is look at the documentation to see a list of all of the callbacks available (e.g. pause
) and then override each one that you need and implement it using the above approach. First, do the normal functions, followed by your custom code.