Flutter | audioplayers onDurationChanged Method isn't working on IOS

806 views Asked by At

I'm trying to make some kind of an mp3 player app and i'm using audioplayers package. And it's been working fine on Android, but on IOS the onDurationChanged doesn't seem to be getting called. And since i'm also showing a slider, it gives an Error on IOS because the max value returns null

Here's my code

class AudioProvider extends ChangeNotifier {
  AudioProvider() {
    initAudio();
  }
  AudioPlayer _audioPlayer = AudioPlayer();

  Duration totalDuration;
  Duration position;
  String audioState;

  initAudio() {
    _audioPlayer.onDurationChanged.listen((updatedDuration) {
      totalDuration = updatedDuration; // This doesn't work on IOS, totalDuration == null
      notifyListeners();
    });

    _audioPlayer.onAudioPositionChanged.listen((updatedPosition) {
      position = updatedPosition;
      notifyListeners();
    });

    _audioPlayer.onPlayerStateChanged.listen((playerState) {
      if (playerState == AudioPlayerState.STOPPED) audioState = "Stopped";
      if (playerState == AudioPlayerState.PLAYING) audioState = "Playing";
      if (playerState == AudioPlayerState.PAUSED) audioState = "Paused";
      notifyListeners();
    });
  }

  playPauseAudio(String url, bool alreadyPlaying) async {
    if (!alreadyPlaying) {
      position = null;
      totalDuration = null;
      await _audioPlayer.play(url);
      notifyListeners();
    }

    if (audioState == 'Playing') {
      await _audioPlayer.pause();
    } else {
      await _audioPlayer.resume();
    }
    notifyListeners();
  }

  void stop() {
    _audioPlayer.stop();
  }

  void seekToSec(Duration durationToSeek) {
    _audioPlayer.seek(durationToSeek);
    notifyListeners();
  }
0

There are 0 answers