Skip to content Skip to sidebar Skip to footer

Exoplayer - Play Local Mp4 File In Sd Card

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 ca

Solution 1:

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:

privatevoidprepareExoPlayerFromFileUri(Uri uri){
        exoPlayer = ExoPlayerFactory.newSimpleInstance(this, newDefaultTrackSelector(null), newDefaultLoadControl());
        exoPlayer.addListener(eventListener);

        DataSpecdataSpec=newDataSpec(uri);
        finalFileDataSourcefileDataSource=newFileDataSource();
        try {
            fileDataSource.open(dataSpec);
        } catch (FileDataSource.FileDataSourceException e) {
            e.printStackTrace();
        }

        DataSource.Factoryfactory=newDataSource.Factory() {
            @Overridepublic DataSource createDataSource() {
                return fileDataSource;
            }
        };
        MediaSourceaudioSource=newExtractorMediaSource(fileDataSource.getUri(),
                factory, newDefaultExtractorsFactory(), 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.

Solution 2:

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

StringplayerInfo= Util.getUserAgent(context, "ExoPlayerInfo");
DefaultDataSourceFactorydataSourceFactory=newDefaultDataSourceFactory(
        context, playerInfo
);
MediaSourcemediaSource=newExtractorMediaSource.Factory(dataSourceFactory)
    .setExtractorsFactory(newDefaultExtractorsFactory())
    .createMediaSource(Uri.parse("asset:///your_video.mov"));
player.prepare(mediaSource);

Solution 3:

This worked for me.Try these steps:

Get path of the file and start the player

FilemyFile=newFile(extStore.getAbsolutePath() + "/folder/videos/" + video_name);  
videoUrl= String.valueOf(Uri.fromFile(myFile));  
initializePlayer(videoUrl);

Initializing player

privatevoidinitializePlayer(String videoUrl) {
    player = ExoPlayerFactory.newSimpleInstance(
            newDefaultRenderersFactory(getActivity()),
            newDefaultTrackSelector(), newDefaultLoadControl());

    playerView.setPlayer(player);

    player.setPlayWhenReady(playWhenReady);
    player.seekTo(currentWindow, playbackPosition);

    Uriuri= Uri.parse(videoUrl);
    MediaSourcemediaSource= buildMediaSource(uri);
    player.prepare(mediaSource, resetPositionBoolean, false);
}   

Building media source

privateMediaSourcebuildMediaSource(Uri uri) {
    returnnewExtractorMediaSource.Factory(
            newDefaultDataSourceFactory(getActivity(),"Exoplayer-local")).
            createMediaSource(uri);
}

Solution 4:

For those looking to load from the assets folder:

assets/xyz.mp4

it works by loading the file with:

"file:/android_asset/xyz.mp4"

and using the DefaultDataSourceFactory. Checked on exoplayer 2.4.4.

Solution 5:

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

Post a Comment for "Exoplayer - Play Local Mp4 File In Sd Card"