flutterlocal-storageaudio-playerflutter-audioplayers

Play music files from Phone's directory


I am developing an application where users can download songs and play them offline. These downloaded songs will remain within the app. I implement the download feature using flutter_downloader: ^1.10.2. Now I want to play that downloaded music in the app using audioplayers: ^3.0.1. How to load music from device storage in flutter?

  final player = AudioPlayer();

await player.play(DeviceFileSource(
                              'data/user/0/{appName}/app_flutter/{fileName}')

This code throws the error - Unexpected error! D/AudioPlayers(12795): java.io.FileNotFoundException: "myFilePath data/user/0/{appName}/app_flutter/{fileName} ": open failed: ENOENT (No such file or directory)


Solution

  • Here is solution for that, Maybe extension was the issue Process to save file

    final dir = await getApplicationDocumentsDirectory();
    var localPath = dir.path + name; //Name : Folder name
    
    await dio.download({content URL},"$localPath/${YourFileName}.$extension",
           onReceiveProgress: (receivedBytes, totalBytes) {
         downloadPercentage.value = ((receivedBytes / totalBytes));
         //Further stuff
       });
    

    Path will be - "$localPath/${YourFileName}.$extension"

    Process to fetch file

    await player.play(DeviceFileSource(
                                  '$localPath/${YourFileName}.$extension')