ryanheise / audio_service

Flutter plugin to play audio in the background while the screen is off.
805 stars 480 forks source link

No clear explaination for using multiple handler #1073

Closed FireLord closed 5 months ago

FireLord commented 5 months ago

Documentation request for the following page(s)

https://pub.dev/packages/audio_service

Section(s) in question

Advanced features Compose multiple audio handler classes:

_audioHandler = await AudioService.init( builder: () => AnalyticsAudioHandler( PersistingAudioHandler( MyAudioHandler())), );

Suggested improvement

There is no function related to AnalyticsAudioHandler, no proper guide to access particular handler when using the play pause function

ryanheise commented 5 months ago

The idea is that you can create your own custom handler as a subclass of CompositeAudioHandler. You could create multiple of these, such as AnalyticsAudioHandler and PersistingAudioHandler. Those are not built-ins, they would be examples of custom classes you would write yourself. You can click on API documentation to read the documentation for CompositeAudioHandler, but I will grant that it is not detailed. However, there is an example in the examples directory.

FireLord commented 5 months ago

Okay with help of example I was able to achieve what I wanted. Thank you @ryanheise for quick response!

Here is code snippet for someone who's looking for same.

  1. You have to create your two or more handler like you normally do
  2. Then create a MainSwitchHandler
    
    import 'package:audio_service/audio_service.dart';

class MainSwitchHandler extends SwitchAudioHandler { final List handlers;

MainSwitchHandler(this.handlers) : super(handlers.first);

@override Future customAction(String name, [Map<String, dynamic>? extras]) async { switch (name) { case 'switchToHandler': stop(); final index = extras!['index'] as int; inner = handlers[index]; return null; default: return super.customAction(name, extras); } } }

3. Then init your AudioService like this. I'm using get_it to inject here
```dart
  sl.registerSingletonAsync<AudioHandler>(() async {
    return AudioService.init(
      builder: () => MainSwitchHandler([
        AudioPlayerHandler(),
        StoryPlayerHandler(),
      ]),
      config: const AudioServiceConfig(
        androidNotificationChannelId: 'com.music.focusflow.channel.audio',
        androidNotificationChannelName: 'Audio playback',
        androidNotificationOngoing: true,
        androidNotificationIcon: 'mipmap/ic_launcher',
      ),
    );
  });
  1. Then create a extension on AudioHandler
    extension AudioHandlerExtension on AudioHandler {
    Future<void> switchToHandler(int? index) async {
    if (index == null) return;
    final AudioHandler audioHandler = sl();
    await audioHandler.customAction('switchToHandler', <String, dynamic>{'index': index});
    }
    }
  2. Then you can switch it like this with respect to order of making the list. Here audioHandler is injected from the get_it.
    audioHandler.switchToHandler(0);
  3. For more details you can check the example here
github-actions[bot] commented 5 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with audio_service.