使用 exoplayer 播放 getExternalFilesDir mp3

Playing getExternalFilesDir mp3 using exoplayer

我是 exoplayer 的新手,我在通过 android 中的外部文件目录播放音频文件(特别是 mp3)时遇到了一些困难。

        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()

它给了我一个错误 - 源错误

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)

我使用 isExist 方法检查音频文件是否存在,return对我来说是正确的。

感谢您的帮助。

android 版本是什么?你得到文件未找到异常<看起来你试图以错误的方式获取文件或者它不存在。如果您使用 android 8+,您应该使用 FileProvider 获取 Uri,而不是 Uri.fromFile() 代码示例:

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

在清单中:

 <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(在 res 文件夹中):

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