ExoPlayer - 在SD卡中播放本地mp4文件 [英] ExoPlayer - play local mp4 file in SD card

查看:4607
本文介绍了ExoPlayer - 在SD卡中播放本地mp4文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Exoplayer演示应用,并希望从SD卡预加载MP4视频。
我已经尝试过这篇文章,但它不起作用。在我的exoplayer Demo中没有名为DemoUtil.java的类。
改为使用:

I am using the Exoplayer Demo app and want to preload a MP4 video from SD card. I have tried out the implementation from this post, but it does not work. There is no such class called DemoUtil.java in my exoplayer Demo. Instead used:

public static final Sample[] LOCAL_VIDEOS = new Sample[] {
new Sample("Some User friendly name of video 1",
"/mnt/sdcard/video1.mp4", Util.TYPE_OTHER),
};

我也无法使用他们为SampleChooserActivity.java提到的代码片段。 (保留给我错误)

I also could not use the their snippet of code mentioned for SampleChooserActivity.java. (Kept giving me errors)

我改为使用:

group = new SampleGroup("Local Videos");
group.addAll(Samples.LOCAL_VIDEOS);
sampleGroups.add(group);

我做错了什么?每个设备的文件路径是否都会更改?

What am I doing wrong? Does the path of the file change for every device?

推荐答案

没有尝试过演示应用,但我已设法创建我自己播放本地音频文件的示例并在此处发布:
https://github.com/nzkozar / ExoplayerExample

Haven't tried the demo app, but I have managed to create my own example of playing local audio files and have posted it here: https://github.com/nzkozar/ExoplayerExample

以下是从文件Uri准备播放器的所有工作的主要部分:

Here is the main part that does all the work of preparing the player from a file Uri:

private void prepareExoPlayerFromFileUri(Uri uri){
        exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(null), new DefaultLoadControl());
        exoPlayer.addListener(eventListener);

        DataSpec dataSpec = new DataSpec(uri);
        final FileDataSource fileDataSource = new FileDataSource();
        try {
            fileDataSource.open(dataSpec);
        } catch (FileDataSource.FileDataSourceException e) {
            e.printStackTrace();
        }

        DataSource.Factory factory = new DataSource.Factory() {
            @Override
            public DataSource createDataSource() {
                return fileDataSource;
            }
        };
        MediaSource audioSource = new ExtractorMediaSource(fileDataSource.getUri(),
                factory, new DefaultExtractorsFactory(), null, null);

        exoPlayer.prepare(audioSource);
    }

你可以得到这样的Uri: Uri。 fromFile(file)

You can get the Uri like this: Uri.fromFile(file)

如上所示准备好文件进行播放后,只需要调用 exoPlayer .setPlayWhenReady(true); 开始播放。

After you have prepared your file for playback as shown above, you only need to call exoPlayer.setPlayWhenReady(true); to start playback.

对于视频文件,您可能只需要将表面视图附加到exoPlayer对象,但我还没有用ExoPlayer2做到这一点。

For a video file you'd probably only need to attach a surface view to your exoPlayer object, but I haven't really done this with ExoPlayer2 yet.

这篇关于ExoPlayer - 在SD卡中播放本地mp4文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆