【发布时间】:2020-10-22 11:18:32
【问题描述】:
我怎样才能根据需要多次播放相同的音频文件,而不是循环播放,但我希望能够精确地播放多少次,我按照tutorial 制作了播放/暂停功能的基础知识。我正在与其他人作斗争。 我使用audioplayers 包的最新版本。
这是我的代码:
import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:audioplayers/audio_cache.dart';
class Player extends StatefulWidget {
@override
_PlayerState createState() => _PlayerState();
}
AnimationController _animationIconController ;
AudioCache audioCache;
AudioPlayer audioPlayer;
bool issongplaying = false;
bool isplaying = false;
class _PlayerState extends State<Player> with TickerProviderStateMixin {
@override
void initState() {
// TODO: implement initState
super.initState();
initPlayer();
}
void initPlayer(){
_animationIconController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 750),
reverseDuration: Duration(milliseconds: 750),
);
audioPlayer = new AudioPlayer();
audioCache = new AudioCache(fixedPlayer: audioPlayer);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
GestureDetector(
onTap: (){
setState((){
isplaying
? _animationIconController.reverse()
: _animationIconController.forward();
isplaying = !isplaying;
});
if (issongplaying == false) {
audioCache.play('audio.wav');
setState(() {
issongplaying = true;
});
} else {
audioPlayer.pause();
setState(() {
issongplaying = false;
});
}
},
child: ClipOval(
child: Container(
decoration: BoxDecoration(
border: Border.all(
width: 2,
color: Colors.greenAccent,
),
borderRadius: BorderRadius.all(Radius.circular(50.0)
),
),
width: 75,
height: 75,
child: Center(
child: AnimatedIcon(
icon: AnimatedIcons.play_pause,
progress: _animationIconController,
color: Colors.grey,
size: 60,
)
),
),
),
)
],
),
),
);
}
}
谢谢
【问题讨论】:
-
一些关于你编码的技巧(我想你刚刚开始编码),条件写得很奇怪,最好使用 if else,还有为什么使用 issongplaying == false,最好使用 !issongplaying。这些事情是不可原谅的。
-
没错,我刚开始学习编码,谢谢你的评论!
标签: flutter dart audio audio-player