Search code examples
androidflutterurlaudioflutter-dependencies

Play Audio in Flutter from URL


I am new to flutter and I want to play audio file from URL path with play, pause and seek button and also show notification in player.


Solution

  • You can use audioplayers package.

    Define AudioPlayer instance like this

    AudioPlayer audioPlayer = AudioPlayer();
    

    Play audio as shown below

      play() async {
        int result = await audioPlayer.play(url);
        if (result == 1) {
          // success
        }
      }
    

    You can pause and stop like this

    await audioPlayer.pause();
    await audioPlayer.stop();
    

    This is how you can play local audio

     playLocal() async {
        int result = await audioPlayer.play(localPath, isLocal: true);
      }
    

    I recommend you to use audio_service package. This is highly customizable with notification and you can also play audio in background or sleep mode.