【发布时间】:2021-12-09 09:48:07
【问题描述】:
我将音频文件保存在列表“文件”中。我想使用 audioplayers 包播放这个音频文件。我已经使用 listview 列出了音频文件,但我只想在多个音频文件中播放一个选定的音频文件。目前是同时播放记录中的整个音频文件,出现错误。
我该如何解决这个问题?
这是我的代码的一部分。
class AudioViewer extends StatefulWidget {
@override
_AudioViewerState createState() => _AudioViewerState();
}
class _AudioViewerState extends State<AudioViewer> {
List file = [];
var audioPath;
var directory;
AudioPlayer? audioPlayer;
bool _isplaying = false;
var _icon = Icons.play_arrow;
var _deleteicon = Icons.delete;
Color _color = Colors.deepOrangeAccent;
Duration position = Duration();
Duration duration = Duration(seconds: 1);
int indexindex = 0;
@override
void initState() {
super.initState();
getFiles();
_setupAudioPlayer();
}
void getFiles() async {
directory = (await getExternalStorageDirectory())!.path;
setState(() {
file = Directory("$directory").listSync();
});
print(file.toString());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
title: Text(
"Audio List",
),
),
body: Container(
child: ListView.builder(
reverse: true,
itemCount: widget.records.length,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return Card(
elevation: 5,
child: ExpansionTile(
title: Text(
widget.records[index].path.split('/').last.split(".").first,
style: TextStyle(color: Colors.black),
),
onExpansionChanged: ((newState) {
if (newState) {
indexindex = index;
setState(() {
});
}
}),
children: [
Container(
height: 100,
padding: EdgeInsets.fromLTRB(10,0,10,0),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Container(
child: IconButton(
iconSize: 40,
onPressed: () {
if (!_isplaying) {
audioPlayer!.resume();
setState(() {
_isplaying = true;
_icon = Icons.pause;
_color = Colors.blueGrey;
});
} else {
audioPlayer!.release();
setState(() {
position = new Duration();
_isplaying = false;
_icon = Icons.play_arrow;
_color = Colors.deepOrangeAccent;
});
}
},
icon: Icon(_icon),
color: _color,
),
),
Container(
child: IconButton(
iconSize: 30,
onPressed: () {
widget.records[index].delete(recursive: true);
setState(() {
widget.records.removeAt(index);
position = new Duration();
});
},
icon: Icon(_deleteicon),
),
),
]
),
【问题讨论】: