ExoPlayer - play local mp4 file in SD card

2019-03-15 10:39发布

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

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

I instead used :

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?

4条回答
做自己的国王
2楼-- · 2019-03-15 11:17

Video playback from sd card worked with following code. My test file is in Videos directory in sdcard.

public static final Sample[] LOCAL_VIDEOS = new Sample[] {
        new Sample("test",
            Environment.getExternalStorageDirectory()+"/Videos/test.mp4", Util.TYPE_OTHER),
};
查看更多
男人必须洒脱
3楼-- · 2019-03-15 11:21

For those who want to play a video from assets using ExoPlayer 2 here is the way:

String playerInfo = Util.getUserAgent(context, "ExoPlayerInfo");
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(
        context, playerInfo
);
MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory)
    .setExtractorsFactory(new DefaultExtractorsFactory())
    .createMediaSource(Uri.parse("asset:///your_video.mov"));
player.prepare(mediaSource);
查看更多
我想做一个坏孩纸
4楼-- · 2019-03-15 11:23

In some devices you could directly used this path '/sdcard/nameoffile.mp4".

查看更多
闹够了就滚
5楼-- · 2019-03-15 11:30

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

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

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

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

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.

查看更多
登录 后发表回答