使用 Flutter 音频播放器播放 URL 时出错

时间:2021-02-09 17:21:12

标签: flutter dart

我只是想测试 Flutter Audioplayers 库,但我在实际播放音频时遇到了问题。代码如下:

指定变量和初始化状态

  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;
      });
    };
  }

在构建中初始化

                     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;
                              });
                            }
                          },

但是当我点击播放按钮时,我在这里收到此错误:

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
                          

我在 manifest.xml 中添加了明文 http,所以它可能不是那个,任何想法它可能是什么?谢谢!

1 个答案:

答案 0 :(得分:0)

您使用过 Flutter 的 Audioplayers 库,但该库不支持流 URL,使用了 audio_stream_player 这个库。