Playing getExternalFilesDir mp3 using exoplayer

Viewed 395

I am fairly new to exoplayer and I having some difficulties playing audio files (specifically mp3) via external files directory in android.

        val trackSelector = DefaultTrackSelector(this)
        trackSelector.setParameters(
            trackSelector.buildUponParameters().setMaxVideoSizeSd()
        )
        val player : SimpleExoPlayer = SimpleExoPlayer.Builder(this).setTrackSelector(trackSelector).build()


        videoView.player = player
        // Create a data source factory.
        val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(this)
        // Create a progressive media source pointing to a stream uri.
        val mediaSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(MediaItem.fromUri(Uri.fromFile(getExternalFilesDir("app_tracks/helloaudio.mp3"))))

        player.setMediaSource(mediaSource)
        player.playWhenReady = true
        player.seekTo(0, 0)
        player.prepare()

And it gave me an error - Source error

E/ExoPlayerImplInternal: Playback error
      com.google.android.exoplayer2.ExoPlaybackException: Source error
        at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:554)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:214)
        at android.os.HandlerThread.run(HandlerThread.java:67)
     Caused by: com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.company.app.android/files/app_tracks/helloaudio.mp3: open failed: EISDIR (Is a directory)
        at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:97)
        at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:193)
        at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:84)
        at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:1013)
        at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:415)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)
     Caused by: com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.mindfi.app2.android/files/app_tracks/helloaudio.mp3: open failed: EISDIR (Is a directory)

I check the audio file whether is exists using isExist method, and it return true to me.

I appreciate your help.

1 Answers

What is the android version? You getting file not found exception< looks like you trying to get your file wrong way or it is not exist. If you are using android 8+ you suppose to use FileProvider to get Uri, not Uri.fromFile() code example:

final Uri uri = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) ?
                FileProvider.getUriForFile(context, packageName + ".provider", file) : Uri.fromFile(file);

in manifest:

 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.yourpackage.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

xml/file_paths (in res folder):

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="." />
</paths>
Related