Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

android - How to play file from external storage in exoplayer?

From assets all working fine! But from external storage no.

This is my exoplayer instance:

// ExoPlayer
        val renderesFactory = DefaultRenderersFactory(
            this,
            DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF
        )
        val trackSelector = DefaultTrackSelector()
        exoPlayer = ExoPlayerFactory.newSimpleInstance(
            this@PlayerService,
            renderesFactory,
            trackSelector
        )
        exoPlayer!!.addListener(exoPlayerListener)

Here I'm trying to build a path:

val dirPath = applicationContext.getExternalFilesDir(audio.uri)!!.absolutePath
val filePath = "$dirPath/001.mp3"
val audioFile = File(filePath)
val uri = Uri.fromFile(audioFile)
prepareToPlay(uri)

audio.uri --> "Downloads/audio_reading/"
And this is prepareToPlay() method:

private fun prepareToPlay(uri: Uri) {
            if (uri != currentUri) {
                currentUri = uri
                val userAgent = Util.getUserAgent(this@PlayerService, "ExoPlayer")
                val mediaSource = ExtractorMediaSource(
                    uri,
                    DefaultDataSourceFactory(this@PlayerService, userAgent),
                    DefaultExtractorsFactory(), null, null
                )
                exoPlayer!!.prepare(mediaSource)
            }
        }

When I start app and click "play" I'm getting that error:

E/ExoPlayerImplInternal: Source error.
    com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.music.app/files/Downloads/audio_reading/001.mp3: open failed: EISDIR (Is a directory)
        at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:73)
        at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:250)
        at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)
        at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.load(ExtractorMediaPeriod.java:886)
        at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:381)
        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:764)
     Caused by: java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.music.app/files/Downloads/audio_reading/001.mp3: open failed: EISDIR (Is a directory)
        at libcore.io.IoBridge.open(IoBridge.java:485)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:288)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:151)
        at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:65)
        at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:250)?
        at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)?
        at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.load(ExtractorMediaPeriod.java:886)?
        at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:381)?
        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:764)?
     Caused by: android.system.ErrnoException: open failed: EISDIR (Is a directory)
        at libcore.io.IoBridge.open(IoBridge.java:475)

Can anybody help?

question from:https://stackoverflow.com/questions/65914475/how-to-play-file-from-external-storage-in-exoplayer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As mentioned in the comments, the error is indicting that the system is seeing the path you are trying to open not as a file but as a directory.

open failed: EISDIR (Is a directory)

You code to open the file looks good so one common reason for an error like this is that the code that actually created or stored the file created a directory rather than a file - e.g. if the following is called:

yourFilePath.mkdirs()

This will actually create a directory called 'yourFilePath' rather than create a directory for a file called 'yourFilePath'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...