ExoPlayer - play local mp4 file in SD card

后端 未结 8 514
囚心锁ツ
囚心锁ツ 2021-02-04 03:49

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

8条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 04:26

    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.

提交回复
热议问题