ExoPlayer reading mp3 file from raw folder

7.9k views Asked by At

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!

4

There are 4 answers

0
Zsolt Boldizsar On BEST ANSWER

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.

0
Zoran On

Simply like this:

    val exoPlayer = ExoPlayer.Builder(context).build()
    val fileUri = RawResourceDataSource.buildRawResourceUri(R.raw.sound1)
    val mediaItem = MediaItem.fromUri(fileUri)
    exoPlayer.setMediaItem(mediaItem)
    exoPlayer.prepare()
    exoPlayer.playWhenReady = true
2
stavros.3p On

The documentation of this library is so bad. It took me hours to write a simple functionality. If anyone is interested, this code works:

public static void playSound(Context context, int sounRes, float volume)
    {
        //example int sounRes=R.raw.duck;
        SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(context);

        DataSpec dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(sounRes));
        final RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(context);
        try {
            rawResourceDataSource.open(dataSpec);
        } catch (RawResourceDataSource.RawResourceDataSourceException e) {
            e.printStackTrace();
        }

        DataSource.Factory factory = () -> rawResourceDataSource;
        ProgressiveMediaSource mediaSource = new ProgressiveMediaSource
                .Factory(factory)
                .createMediaSource(rawResourceDataSource.getUri());

        player.prepare(mediaSource);
        player.setPlayWhenReady(true);
        player.setVolume(volume);

        player.addListener(new Player.EventListener() {
            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                if (playbackState == Player.STATE_ENDED) {
                    player.release();
                }
            }
        });
    }

If you want to use an asset file, then use this:

public static void playSound(Context context,String audioPath,float volume)
{
    //example String audioPath = "duck.mp3";
    SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(context);

    DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(context,
            Util.getUserAgent(context, "exoPlayerSample"));

    ProgressiveMediaSource mediaSource = new ProgressiveMediaSource
            .Factory(dataSourceFactory)
            .createMediaSource(Uri.parse("asset:///" + audioPath));

    player.prepare(mediaSource);
    player.setPlayWhenReady(true);
    player.setVolume(volume);

    player.addListener(new Player.EventListener() {
        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            if(playbackState == Player.STATE_ENDED){
                player.release();
            }
        }
    }); 
}
2
Aaron He On

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)