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