ExoPlayer从原始文件夹中读取mp3文件

时间:2015-06-15 18:58:45

标签: android exoplayer

是否有可能将位于应用程序原始文件夹中的mp3文件设置为ExoPlayer?

我试图用以下代码片段实现它,但遗憾的是没有成功:

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

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:3)

可以从原始文件夹加载文件,关键是使用RawSourceDataSource

这是一个示例(在Kotlin中)为原始目录中的mp3文件创建LoopingMediaSource

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)

答案 1 :(得分:1)

我无法从原始文件加载mp3文件,所以我最终根据与ExoPlayer的一位作者的讨论将它们移动到资源目录。 (https://github.com/google/ExoPlayer/issues/556

这是我从资产中访问mp3文件的方式,如果有人将来需要它:

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

并将此路径添加到DemoPlayer中,如下所示:

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

感谢所有人愿意回答我的问题。

答案 2 :(得分:1)

该库的文档太糟糕了。我花了几个小时来编写一个简单的功能。如果有人感兴趣,此代码将起作用:

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();
                }
            }
        });
    }

如果您要使用资产文件,请使用此文件:

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();
            }
        }
    }); 
}