Flutter-AssetsAudioPlayer icon indicating copy to clipboard operation
Flutter-AssetsAudioPlayer copied to clipboard

Can get duration on android but not web

Open sgjohnson1981 opened this issue 2 years ago • 0 comments

Flutter Version

My version : 3.7.9

Lib Version

My version : 3.0.6

Platform (Android / iOS / web) + version

Platform : web

Describe the bug

Can get a Duration object from a stream when using an android emulator (SDK 28), but can't get one on web. Or it sends one but it's 0.000.... Therefore the "pause" within the loop doesn't work correctly because it's what I'm depending on to determine the pause length between clips.

Small code to reproduce

for (var clip in clipsToLoad) {
      // Play through clips
      if (_isDisposed) {
        break;
      }
      try { // Try loading clip
        await _player.open(Audio(clip));
        print("$clip set");
      } catch (e) {
        print("Error loading audio source: $e");
      }
      if (_isDisposed) {
        break;
      }
      try { // Try playing
        await _player.play();
        print("$clip played");
      } catch (e) {
        print("Could not play clip");
      }
      if (_isDisposed) {
        break;
      }
      try { // Try getting duration

        var playingAudio = await _player.current.firstWhere((playingAudio) => playingAudio?.audio.duration != null);
        _duration = playingAudio?.audio.duration;
        if (_duration == null) {
          throw Exception("_duration is null");
        }
        print("duration: $_duration");
      } catch (e) {
        print("Could not get duration: $e");
      }
      if (_isDisposed) {
        break;
      }
      try { // Try pausing for X seconds
        await Future.delayed(Duration(milliseconds: _duration!.inMilliseconds + (pauseInt * 1000)));
      } catch (e) {
        print("Could not delay");
      }
      if (_isDisposed) {
        break;
      }
      try { // Try stopping
        _player.stop();
      } catch (e) {
        print("Could not stop()");
      }
    }

sgjohnson1981 avatar Apr 06 '23 21:04 sgjohnson1981