Is it possible to mix-in ChangeNotifier with audio_service in Flutter?

47 views Asked by At

Is it possible to mix-in ChangeNotifier using provider with a BaseAudioHandler class from audio_service package -- so that widgets listen to custom variable changes from the BaseAudioHandler class (specifically the position of a custom timer in my case); in addition to listening to standard variables accessible from a PlaybackEventStream.

I've set up below which doesn't throw an error, but Consumer is not listening to variable changes.

class CustomAudioHandler extends BaseAudioHandler with ChangeNotifier  {
  final audioPlayer = AudioPlayer();

  CountdownTimer? timer;

  int remainingTime = 0;

  CustomAudioHandler() {
    audioPlayer.playbackEventStream
        .map(updatePlaybackState)
        .pipe(playbackState);
  }

  PlaybackState updatePlaybackState(PlaybackEvent event) {
/// etc.
  }

  @override
  Future<void> playMediaItem(MediaItem mediaItem) async {

    timer = CountdownTimer(
        Duration(milliseconds: 60000), Duration(milliseconds: 1));

    timer?.listen((event) {
      remainingTime = event.remaining.inMilliseconds;

    });
    notifyListeners();
  }
///etc.

}

Then listen to this from the app somewhere like so:

Consumer<CustomAudioHandler>(

      builder: (BuildContext context, value, Widget? child) {
        print(value.remainingTime);
/// No variable changes are recorded

ChangeNotifierProvider is set up too (and the BaseAudioHandler class is initialized).


ChangeNotifierProvider(
    create: (_) => CustomAudioHandler(),
    child: etc....
0

There are 0 answers