我將音頻文件保存在列表“文件”中。我想使用 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),
),
),
]
),
似乎您的狀態變量對於整個頁面/列表_isplaying
只有一次。你每首歌都需要它們。這似乎不是一個完整的例子(widget.records
似乎丟失了)所以我在這裡真的幫不上什麼忙。
如果widget.records
包含歌曲,您可以做的是擁有一個currentlyPlaying
變量,即當前正在播放的記錄,而不僅僅是一個通用的_isplaying
. 這樣,您就可以確定當前構建的列表條目是否正在播放。但是也許我誤解了一些東西,因為您的音頻播放器實際上並沒有播放那首歌,它只是恢復之前播放的內容。
也許從簡單的步驟開始,然後首先使圖標起作用。這應該可以通過我上面提到的方法實現:跟踪選擇了哪條記錄。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句