我只是想测试Flutter Audioplayer库,但我在实际播放音频时遇到了问题。
指定变量和初始化状态
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条答案
按热度按时间xu3bshqb1#
您已经使用了Flutter的Audioplayer库,但此库不支持流URL,因此使用了audio_stream_player此库。