android ExoPlayer从原始文件夹中阅读mp3文件

j5fpnvbx  于 2023-11-15  发布在  Android
关注(0)|答案(4)|浏览(183)

有没有可能将位于应用程序原始文件夹中的mp3文件设置为ExoPlayer?
我试图用下面的代码片段来实现它,但不幸的是没有成功:

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

字符串
任何帮助都非常感谢!

polhcujo

polhcujo1#

可以从raw文件夹加载文件,关键是使用RawSourceDataSource
下面是一个例子(在Kotlin中),为raw目录中的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)

字符串

lmyy7pcs

lmyy7pcs2#

这个库的文档太糟糕了。我花了几个小时才写了一个简单的功能。如果有人感兴趣,这段代码可以工作:

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

axzmvihb

axzmvihb3#

我无法从原始文件中加载mp3文件,所以我最终将它们移动到assets目录,这是根据与ExoPlayer作者之一的讨论。
这就是我如何从资产中访问mp3文件,如果有人将来需要它:

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

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

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


谢谢大家愿意回答我的问题。

zphenhs4

zphenhs44#

就像这样:

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

字符串

相关问题