How to play an audio from URL in flutter?

Viewed 6478

I have tried the following code but it didn't work.

    AudioPlayer _audioPlayer = AudioPlayer();

  play() async {
    int result = await _audioPlayer.play('http://thedemos.in/mind/uploads/audio/WjYYX_Namaste2.mp3');
    if (result == 1) {
      setState(() {
        isPlaying = true;
      });
      print('Success');
    }
  }
2 Answers

assets_audio_player 2.0.7+9 use this music player, Where you can play music from the network as well as from assets folder. And Also you don't need to handle it in the background.

audioplayers: ^0.20.1

import 'package:audioplayers/audioplayers.dart';

var audio = AudioPlayer();

Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        IconButton(
            icon: Icon(Icons.play_arrow),
            onPressed: () {
              audio.play(
                  'http://119.226.104.170/Rsoft5140/66119514-20200506-183433-1588770273.71547.mp3');
            }),
        IconButton(
            icon: Icon(Icons.pause),
            onPressed: () {
              audio.pause();
            }),
        IconButton(
            icon: Icon(Icons.stop),
            onPressed: () {
              audio.stop();
            }),
      ],
    ),
Related