I am new to flutter/dart. I am trying to develop a Music Player application.
Problem is when my application trying to retrieve all mp3 files from downloads folder.
It lists all audio(mp3) files on emulator, but when I installed APK on my device the button is stuck when pressed.
What can I do ?
[Emulator : Android 13 Tiramisu]
[Device: Android 11 R]
//requesting permission code
requestPermission() async {
// Web platform don't support permissions methods.
if (!kIsWeb) {
bool permissionStatus = await _audioQuery.permissionsStatus();
if (!permissionStatus) {
await _audioQuery.permissionsRequest();
}
setState(() {});
}
}
//Button code
IconButton(
icon: const Icon(Icons.menu_rounded, size: 30,),
onPressed: () {
Navigator.push(context,MaterialPageRoute(builder: (context) => const Songs()));
},)
class _SongsState extends State<Songs> {
body: SafeArea(
minimum: const EdgeInsets.fromLTRB(5, 10, 5, 5),
child: Column(
children: [
Expanded(
child:ListView.builder(
itemCount: getSongList()[0].length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(getSongList()[1][index].split('/').last,style:
const TextStyle(
fontSize: 21
),),
leading: IconButton(onPressed: (){
Navigator.push(context,MaterialPageRoute(
builder: (context) => music_player(selectedSong: getSongList()[1],selectedIndex:index)
));
},
icon: const Icon(Icons.play_circle,size: 30,)),
// Removed all brackets to reduce code for SO question
// function to retrieve all mp3's
List getSongList(){
Directory dir = Directory('/storage/emulated/0/Download/');
String mp3Path = dir.toString();
List<FileSystemEntity> _files;
List<FileSystemEntity> _songs = [];
List<String> _songpaths = [];
_files = dir.listSync(recursive: true, followLinks: false);
for(FileSystemEntity entity in _files) {
String path = entity.path;
if(path.endsWith('.mp3')) {
_songs.add(entity);
_songpaths.add(path);
}
}
return [_songs,_songpaths];
}
}
2
Answers
Basic problem is you are trying to get list of audio files in build method in sync.
You need to do it as async task. Something like this would work…
Try this to get music list
Then to show music list
Kindly check this article
Also you can use this package for more features