0

I'm trying to play any audio file from local storage in my android app (music player). for obtaining audio files I use MediaStore.query to obtain audio file data, and for playing them I want to use the same data provided earlier.

this is how I'm currently doing this:

Song data class:

data class Song(
    val id: Long,
    val title: String,
    val album: String,
    val artist: String,
    val duration: Long,
    val uri: Uri)

and this is how ti Song.uri property is set:

val contentUri =
            ContentUris.withAppendedId(
                Media.EXTERNAL_CONTENT_URI,
                id
            )
// example of uris: content://media/external/audio/media/259

but when I convert the song objects to MediaItem objects and I try to play them I get a file not found exception with stacktrace:

Playback error
  androidx.media3.exoplayer.ExoPlaybackException: Source error
      at androidx.media3.exoplayer.ExoPlayerImplInternal.handleIoException(ExoPlayerImplInternal.java:684)
      at androidx.media3.exoplayer.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:656)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loopOnce(Looper.java:210)
      at android.os.Looper.loop(Looper.java:299)
      at android.os.HandlerThread.run(HandlerThread.java:67)
  Caused by: androidx.media3.datasource.FileDataSource$FileDataSourceException: java.io.FileNotFoundException: 937: open failed: ENOENT (No such file or directory)
      at androidx.media3.datasource.FileDataSource.openLocalFile(FileDataSource.java:205)
      at androidx.media3.datasource.FileDataSource.open(FileDataSource.java:116)
      at androidx.media3.datasource.DefaultDataSource.open(DefaultDataSource.java:272)
      at androidx.media3.datasource.StatsDataSource.open(StatsDataSource.java:86)
      at androidx.media3.exoplayer.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:1006)
      at androidx.media3.exoplayer.upstream.Loader$LoadTask.run(Loader.java:414)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
      at java.lang.Thread.run(Thread.java:1012)
  Caused by: java.io.FileNotFoundException: 937: open failed: ENOENT (No such file or directory)
      at libcore.io.IoBridge.open(IoBridge.java:574)
      at java.io.RandomAccessFile.<init>(RandomAccessFile.java:289)
      at java.io.RandomAccessFile.<init>(RandomAccessFile.java:152)
      at androidx.media3.datasource.FileDataSource.openLocalFile(FileDataSource.java:186)
      at androidx.media3.datasource.FileDataSource.open(FileDataSource.java:116) 
      at androidx.media3.datasource.DefaultDataSource.open(DefaultDataSource.java:272) 
      at androidx.media3.datasource.StatsDataSource.open(StatsDataSource.java:86) 
      at androidx.media3.exoplayer.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:1006) 
      at androidx.media3.exoplayer.upstream.Loader$LoadTask.run(Loader.java:414) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637) 
      at java.lang.Thread.run(Thread.java:1012) 
  Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
      at libcore.io.Linux.open(Native Method)
      at libcore.io.ForwardingOs.open(ForwardingOs.java:563)
      at libcore.io.BlockGuardOs.open(BlockGuardOs.java:274)
      at libcore.io.ForwardingOs.open(ForwardingOs.java:563)
      at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8125)
      at libcore.io.IoBridge.open(IoBridge.java:560)
      at java.io.RandomAccessFile.<init>(RandomAccessFile.java:289) 
      at java.io.RandomAccessFile.<init>(RandomAccessFile.java:152) 
      at androidx.media3.datasource.FileDataSource.openLocalFile(FileDataSource.java:186) 
      at androidx.media3.datasource.FileDataSource.open(FileDataSource.java:116) 
      at androidx.media3.datasource.DefaultDataSource.open(DefaultDataSource.java:272) 
      at androidx.media3.datasource.StatsDataSource.open(StatsDataSource.java:86) 
      at androidx.media3.exoplayer.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:1006) 
      at androidx.media3.exoplayer.upstream.Loader$LoadTask.run(Loader.java:414) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637) 
      at java.lang.Thread.run(Thread.java:1012) 

which clearly states that the file does not exist, however I am able to load and show the music data (title, artwork, artist name etc) using the same uri.

  • I have checked the permissions in my manifest file and I believe i have the sufficient permission (READ_EXTERNAL_STORAGE)
  • I have seen some similar functions in other android apps: example and they seem to have passed the id to uri field which I have also trid.
  • I have used uri.toString() instead of uri

and obviously non of the above have worked.

  • If your app targets Android 13 or higher try to use READ_MEDIA_AUDIO permission. – sdex Aug 27 '23 at 04:55
  • @sdex I checked the permissions and after many hours of debugging it seems that the problem here was that `Exoplayer` expects to read the uri from what yout set in `setMediaId` which is quite strange and seems like a bad method naming to me. – Farbod Bijary Aug 29 '23 at 14:47

0 Answers0