Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExoPlayer reading mp3 file from raw folder

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!

like image 449
Zsolt Boldizsár Avatar asked Jun 15 '15 18:06

Zsolt Boldizsár


1 Answers

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)
like image 140
Aaron He Avatar answered Nov 03 '22 00:11

Aaron He