fluttermedia-playeraudio-playerjust-audioflutter-audioplayers

How to show artMusic on Media control (Notification) from assets file in flutter


I am using Just Audio and Just Audio Background for playing music and media control on Notification.

I am able to show album, title of storage songs on Notification control but unable to show artmusic of music on Notification.

I am using on Audio query for fetching device music list and displaying in list.

 player.value.setAudioSource(AudioSource.uri(
         Uri.parse(uri),
         tag: MediaItem(
           // Specify a unique ID for each media item:
           id: id.toString(),
           // Metadata to display in the notification:
           album: album.toString(),
           title: title.toString(),
           artUri: Uri.parse("https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg"),
         ),
       ));

I am able to parse any http url of image but unable parse asset image file path in artMusic . Kindly suggest any solutions.


Solution

  • I resolved the issues in this way.

     sources.add(AudioSource.uri(Uri.parse(song.uri!),
              tag: MediaItem(
                // Specify a unique ID for each media item:
                id: song.id.toString(),
                // Metadata to display in the notification:
                album: song.album.toString(),
                title: song.title.toString(),
                artist: song.artist.toString(),
                artUri: await  getImageFileFromAssets() ,
              )));
    

    Now create a method getImageFileFromAssets() and pass the path of assets images.

     Future<Uri> getImageFileFromAssets() async {
        final byteData = await rootBundle.load('assets/music.png');
        final buffer = byteData.buffer;
        Directory tempDir =  await getApplicationDocumentsDirectory();
        String tempPath = tempDir.path;
        var filePath =
            tempPath + '/file_01.png'; // file_01.tmp is dump file, can be anything
        return (await File(filePath).writeAsBytes(
            buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes)))
            .uri;
      }