1

I have list of file path like this one: content://media/external/audio/media/2732 I want to play the audio using the given file path. How can I add that path as a media item?

MediaItem(
    id: "content://media/external/audio/media/2732",
    title: "Test Song",
)

Above code doesn't work.

Finally I found the solution. I used this package: URI to File.

Implementation

Uri uri = Uri.parse("content://media/external/audio/media/2732");
File file = await toFile(uri);
MediaItem(
    id: file.path,
    title: "Test Song",
)
Yubaraj Shrestha
  • 864
  • 1
  • 10
  • 16
  • The tag `just_audio` says: "just_audio is a Flutter plugin for playing audio from files, assets, URLs and byte streams." Because of it being a plugin, you should write some Flutter code to play audio. One example is [How to play a custom sound in Flutter?](https://stackoverflow.com/questions/43813386/how-to-play-a-custom-sound-in-flutter) – Luuk Dec 04 '21 at 09:46
  • 1
    @Luuk I think you misunderstood, my application is working perfectly in case of audio files fetched from internet. Now I wanted to play the songs those were in my device. I am able to get the list of songs but couldn't make it play. – Yubaraj Shrestha Dec 04 '21 at 09:50
  • see: [How to play local audio with audio_service package in Flutter](https://stackoverflow.com/questions/65123009/how-to-play-local-audio-with-audio-service-package-in-flutter) – Luuk Dec 04 '21 at 10:01
  • That's a demo of network file and asset file not local file but anyway thanks for your support even that didn't help much. – Yubaraj Shrestha Dec 04 '21 at 18:02
  • If I understand correctly, the question is about `content://` scheme URIs. There are two Flutter packages that can help you with that. One is [content_provider](https://pub.dev/packages/content_provider) and another upcoming one is [android_content_provider](https://github.com/nt4f04uNd/android_content_provider) (although this requires the master channel of Flutter). – Ryan Heise Dec 05 '21 at 16:24
  • Well this package helped me solve the issue: [URI to File](https://pub.dev/packages/uri_to_file). – Yubaraj Shrestha Dec 06 '21 at 09:19
  • Great - if you like, you can also consider writing an answer below and accepting your own answer. – Ryan Heise Dec 09 '21 at 05:22

3 Answers3

3

Use it like this.

Uri.parse('asset:///assets/audios/song.mp3');
John vinith
  • 114
  • 5
0

Finally I found the solution. I used this package: URI to File.

Implementation

Uri uri = Uri.parse("content://media/external/audio/media/2732");
File file = await toFile(uri);
MediaItem(
    id: file.path,
    title: "Test Song",
)
Yubaraj Shrestha
  • 864
  • 1
  • 10
  • 16
0

you should try to add asset:/// in the start of your string.

example :

before : Uri.parse("content://media/external/audio/media/2732");

after : Uri.parse("asset:///media/external/audio/media/2732");

You can read that if you want : https://github.com/ryanheise/just_audio/blob/master/just_audio/example/lib/example_playlist.dart

Community
  • 1
  • 1