Error Playing Url with Flutter Audioplayers

869 Views Asked by At

I am just trying to test Flutter Audioplayers library, but I am having an issue actually playing the audio. Here's the code:

specifying vars and init state

  class _HomeState extends State<Home> {
  bool playing = false;
  IconData playBtn = Icons.play_arrow;
  AudioPlayer _player;
  AudioCache cache;
  Duration position = new Duration();
  Duration musicLength = new Duration();

  Widget slider() {
    return Container(
      width: 300.0,
      child: Slider.adaptive(
          activeColor: Colors.blue[800],
          inactiveColor: Colors.grey[350],
          value: position.inSeconds.toDouble(),
          max: musicLength.inSeconds.toDouble(),
          onChanged: (value) {
            seekToSec(value.toInt());
          }),
    );
  }

  void seekToSec(int sec) {
    Duration newPos = Duration(seconds: sec);
    _player.seek(newPos);
  }

  //Now let's initialize our player
  @override
  void initState() {
    super.initState();
    _player = AudioPlayer();
    cache = AudioCache(fixedPlayer: _player);

    // ignore: deprecated_member_use
    _player.durationHandler = (d) {
      setState(() {
        musicLength = d;
      });
    };
    // ignore: deprecated_member_use
    _player.positionHandler = (p) {
      setState(() {
        position = p;
      });
    };
  }

initializing it in build

                     IconButton(
                          iconSize: 62.0,
                          color: Colors.blue[800],
                          onPressed: () {
                            //here we will add the functionality of the play button
                            if (!playing) {
                              //now let's play the song
                              _player.play(
                                  "https://www.youtube.com/watch?v=5qap5aO4i9A");
                              setState(() {
                                playBtn = Icons.pause;
                                playing = true;
                              });
                            } else {
                              _player.pause();
                              setState(() {
                                playBtn = Icons.play_arrow;
                                playing = false;
                              });
                            }
                          },

But when I click the play button, I am getting this error here:

V/MediaHTTPService( 7522): MediaHTTPService(android.media.MediaHTTPService@3e1520e): Cookies: null
V/MediaHTTPService( 7522): makeHTTPConnection: CookieManager created: java.net.CookieManager@2c4642f
V/MediaHTTPService( 7522): makeHTTPConnection(android.media.MediaHTTPService@3e1520e): cookieHandler: java.net.CookieManager@2c4642f Cookies: null
E/MediaPlayerNative( 7522): error (1, -2147483648)
E/MediaPlayer( 7522): Error (1,-2147483648)
E/MediaPlayerNative( 7522): stop called in state 0, mPlayer(0xbb5df660)
E/MediaPlayerNative( 7522): error (-38, 0)
V/MediaPlayer( 7522): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
                          

I have added the cleartext http in manifest.xml, so it is probably not that, any ideas what it could be? Thanks!

1

There are 1 best solutions below

1
On

You have used the Audioplayers library of Flutter, But this Library does not support the stream URL, used the audio_stream_player this library.