0

Is there a way to track if we are triggering rewind and fast forward and skipNext and skip to previous?

ZlatiP
  • 185
  • 6
  • Can you clarify what you mean by "we are triggering"? Do you mean if you are calling the methods yourself as opposed to these methods being called back from a notification button click or similar? – Ryan Heise Mar 15 '22 at 15:08
  • I mean calling those methods from my player buttons, not from the notification. – ZlatiP Mar 15 '22 at 22:57
  • So manually calling the methods. In that case, it's simple because you know exactly when you call one of those methods because you wrote code to call it. You can insert your tracking code there. If you want something more generic, you could do it with a composite audio handler which is used whenever manually calling the methods, while the inner handler that it wraps around is the one that is passed into AudioService.init. I can write that up in an answer if it sounds like it solves your issue. – Ryan Heise Mar 16 '22 at 13:13
  • Well, i just created a stream that is sort of a event bus. And push to it the events `@override Future fastForward() async { playerEvents.add(PlayerEvents.forwarding); super.fastForward(); }` and also in the broadcastState added this `if (playing) { playerEvents.add(PlayerEvents.playing); }` So that the playing state is restored immediately after the forwarding has ended. But it feels a little bit odd. – ZlatiP Mar 17 '22 at 18:31
  • Would you like me to write up an answer on composite audio handler? – Ryan Heise Mar 18 '22 at 08:56
  • Would be helpful – ZlatiP Mar 18 '22 at 11:37

1 Answers1

1

You can make an audio handler that wraps around your main audio handler. Your main audio handler is passed into AudioService.init, but your wrapper audio handler is used everywhere else whenever you want to manually trigger a method like fastForward. You can create the wrapper like this:

class FrontendAudioHandler extends CompositeAudioHandler {
  FrontendAudioHandler(AudioHandler inner) : super(inner);

  @override
  Future<void> fastForward() async {
    // insert any code here that should only happen when the app
    // manually invokes fastForward.
    
    await super.fastForward();
  }

  // Override any other methods you need to intercept.
}

Now in your app's startup, you set up two audio handlers, one wrapping the other:

final mainAudioHandler = AudioService.init(builder: () => MainAudioHandler());
final frontendAudioHandler = FrontendAudioHandler(mainAudioHandler);

When the user clicks fast forward in the notification, it will go directly to mainAudioHandler. But when you want to manually trigger a fast forward from within your Flutter code, just make sure you go through frontendAudioHandler, like this:

await frontendAudioHandler.fastForward();

This will first call the fastForward implementation above, giving you a chance to handle the manual trigger, but then that will call super.fastForward() which basically forwards on the request to your main audio handler.

Ryan Heise
  • 2,273
  • 5
  • 14