'flutter:: Is there a way to play only one audio file using the audioplayer package?
I saved the audio files in list 'file'. I want to play this audio file using the audioplayers package. I have listed audio files using listview, but I want to play only one selected audio file among multiple audio files. Currently, the entire audio file in the records is played at the same time and an error has occurred.
How can I solve this?
This is part of my code.
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),
),
),
]
),
Solution 1:[1]
You need to create a globals.dart file like that:
AudioPlayer? _player;
Future<void> playAudio(AudioPlayer player, AudioSource source) async {
if (_player != null && _player!.playing) {
await _player!.stop();
}
_player = player;
player.setAudioSource(audioSource);
player.play();
}
}
And use it in somewhere:
playAudio(currentAudio, audioSource);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | searching9x |
