在pubspec.yaml中添加audioplayers: ^0.19.0之后在lib中添加部件文件,文件中
class AudioFile extends StatefulWidget {
final AudioPlayer advancedPlayer;
const AudioFile({Key key, this.advancedPlayer}) : super(key: key);
@override
_AudioFileState createState() => _AudioFileState();
}
class _AudioFileState extends State<AudioFile> {
// 缓冲时长
Duration _duration = Duration();
// 播放时长
Duration _position = Duration();
// 播放链接地址
final String path = 'https://luan.xyz/files/audio/ambient_c_motion.mp3';
// 是否正在播放
bool isPlaying = false;
// 是否已暂停
bool isPaused = false;
// 是否循环
bool isLoop = false;
// 播放按钮图片
List<IconData> _icons = [
Icons.play_circle_fill,
Icons.pause_circle_filled,
];
@override
void initState() {
super.initState();
this.widget.advancedPlayer.setUrl(path);
// 缓冲时长触发
this.widget.advancedPlayer.onDurationChanged.listen((d) {
setState(() {
_duration = d;
});
});
// 播放时长触发(通常用于播放进度条的使用)
this.widget.advancedPlayer.onAudioPositionChanged.listen((p) {
setState(() {
_position = p;
});
});
// widget.advancedPlayer.onPlayerError.listen((msg) {
// print('audioPlayer error : $msg');
// setState(() {
// _duration = Duration(seconds: 0);
// _position = Duration(seconds: 0);
// });
// });
}
@override
Widget build(BuildContext context) {
return Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
_loadAssets(),
],
),
);
}
Widget _startBtn() {
return IconButton(
padding: EdgeInsets.only(bottom: 10.0),
onPressed: () {
_play();
},
icon: Icon(Icons.play_circle_filled),
);
}
Future _play() async {
int result = await widget.advancedPlayer.play(path,isLocal: false);
if (result == 1) {
// success
print('play success');
} else {
print('play failed');
}
}
Widget _loadAssets() {
return Container(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_startBtn(),
],
),
);
}
}
使用的是iOS虚拟机播放,在iOS文件下的info.plist文件中添加网络访问权限
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
为以防万一我到ios文件夹下做了个pod install.
播放文件为https线上音频文件。
是不是以为我来写技术文了?
当然不是!我都没写暂停paused!!
惭愧啊,我是来提问的,做完上面的工作后我的虚拟机没有声音,对,点击播放没有声音,我还没找到原因,道友有解请留下你的解决方法,不胜感激!
网友评论