使用Flutter audioplayer播放Url时出错



我只是想测试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;
});
};
}

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

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

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,所以它可能不是,任何想法它可能是什么?谢谢!

您已经使用了Flutter的Audioplayers库,但是这个库不支持流URL,请使用audio_stream_player这个库。

最新更新