Search code examples
iosflutteraudiohttpsaudio-player

audioplayer playing inconsistent / weird audio (url mp3 link) result in flutter


following the instructions from: https://codingwithjoe.com/playing-audio-from-the-web-and-http/ (using his example and AudioProvider class)

I have been using:

  audioplayer: ^0.8.1
  audioplayer_web: ^0.7.1

to play audio from https link.

The problem it has some weird inconsistent effect. - after playing few audio, it keeps on playing the same audio eventhough new url is loaded - after playing few audio, the sound is weird like some part is cut with other audio.

What is a good audio player that accepts url link for flutter that can produce a consistent result ?


Solution

  • the provided audioplayer from your example works great and has good features. From what you are describing for me it seems like you're not closing the session when you play a sound. It seems like you are stacking the sounds which causes weird sounds.

    You have to close the instance then. even though the article is outdated (march 2018) the audioplayer has developed further. check their offical guide here:

    https://pub.dev/packages/audioplayer

    This is version audioplayer 0.8.1 not 3.0 or something..

    Example from docs:

    Instantiate an AudioPlayer instance
    //...
    AudioPlayer audioPlugin = AudioPlayer();
    //...
    

    Player controls:

    audioPlayer.play(url);
    
    audioPlayer.pause();
    
    audioPlayer.stop();
    

    status and current position:

    //...
    _positionSubscription = audioPlayer.onAudioPositionChanged.listen(
      (p) => setState(() => position = p)
    );
    
    _audioPlayerStateSubscription = audioPlayer.onPlayerStateChanged.listen((s) {
      if (s == AudioPlayerState.PLAYING) {
        setState(() => duration = audioPlayer.duration);
      } else if (s == AudioPlayerState.STOPPED) {
        onComplete();
        setState(() {
          position = duration;
        });
      }
    }, onError: (msg) {
      setState(() {
        playerState = PlayerState.stopped;
        duration = new Duration(seconds: 0);
        position = new Duration(seconds: 0);
      });
    });
    

    Like I said most of the audio plugins are running in singleton mode with instances. To provide getting weird effects you have to load the next song in the same instance, don't open another new instance, and you wont get any weird effects.

    If you want to switch to a different audio player another great one which I used in an app project is the following:

    https://pub.dev/packages/audio_manager#-readme-tab-

    Hope it helps.