ExoPlayer reading mp3 file from raw folder

荒凉一梦 提交于 2019-12-07 13:56:10

问题


Is there any possibility to set an mp3 file that's located in the app's raw folder to ExoPlayer?

I tried to achieve it with the following code snippet without success unfortunately:

mMediaPath = "android.resource://" + getPackageName() + File.separator + R.raw.ringtone;

Any help is greatly appreciated!


回答1:


It's possible to load files from the raw folder, the key is to use RawSourceDataSource.

Here's an example(in Kotlin) to create a LoopingMediaSource for an mp3file in the raw directory:

val uri = RawResourceDataSource.buildRawResourceUri(R.raw.mp3file)
val dataSource = RawResourceDataSource(this)
dataSource.open(DataSpec(uri))

val source = ExtractorMediaSource(uri, DataSource.Factory { dataSource }, Mp3Extractor.FACTORY, null, null)

LoopingMediaSource(source)



回答2:


I couldn't load the mp3 files from the raw files so I ended up moving them to assets directory as per the discussion with one of the authors of ExoPlayer. (https://github.com/google/ExoPlayer/issues/556)

This is how I accessed the mp3 files from the assets if somebody will need it in the future:

mMediaPath = "asset:///my_ringtone.mp3";

and added this path the DemoPlayer as follows:

new DemoPlayer(new ExtractorRendererBuilder(this, userAgent, Uri.parse(mMediaPath), null, new Mp3Extractor()));

Thanks to all willing to answer my question.



来源:https://stackoverflow.com/questions/30852975/exoplayer-reading-mp3-file-from-raw-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!