ExoPlayer reading mp3 file from raw folder
Asked Answered
I

4

8

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!

Incognizant answered 15/6, 2015 at 18:58 Comment(4)
Yes. The assets put into the raw folder do not get touched by APPT. There are many ways to achieve this; but first, is your raw folder inside the res folder?Just
Yes it is inside the res folder.Incognizant
@GeorgeD have you found any possibility?Incognizant
Do you get an Exception or any error in the logs? Can you post more code of how you create and prepare your ExoPlayer and audio renderer instances for example?Claireclairobscure
I
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.

Incognizant answered 22/7, 2015 at 10:14 Comment(0)
C
7

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)
Cush answered 12/7, 2017 at 22:26 Comment(3)
How would that read in Java? I want to MergeMediaSource() that, so that it could be used to blend local samples into a running steam…Topcoat
ExtractorMediaSource was deprecatedSeldan
buildRawResourceUri is deprecated, use Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE).path(Integer.toString(rawResourceId)).build() as documentedPhotographic
T
4

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();
            }
        }
    }); 
}
Tsana answered 3/4, 2020 at 7:14 Comment(2)
Because ExtractorMediaSource was deprecated and this works with ProgressiveMediaSource, so your answer is the best!Seldan
The prepare(mediaSource) doesn't exist now, and createMediaSource can't take just Uri, but this might be fine using `MediaItem.fromUri(rawResourceDataSource.uri) instead. This can't be used now...Skiing
I
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.

Incognizant answered 22/7, 2015 at 10:14 Comment(0)
R
0

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
Rus answered 10/11, 2023 at 12:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.