ExoPlayer - play local mp4 file in SD card

后端 未结 8 466
囚心锁ツ
囚心锁ツ 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:25

    For those who trying to play a video file from res/raw* folder, here is the solution. Keep in mind that i used the **2.8.0 version of the ExoPlayer.

    public class MainActivity extends AppCompatActivity {
    
    PlayerView playerView;
    SimpleExoPlayer simpleExoPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playerView=findViewById(R.id.playerView);
    }
    
    @Override
    protected void onStart() {
        simpleExoPlayer= ExoPlayerFactory.newSimpleInstance(this,new DefaultTrackSelector());
        DefaultDataSourceFactory defaultDataSourceFactory=new DefaultDataSourceFactory(this, Util.getUserAgent(this,"YourApplicationName"));
        simpleExoPlayer.setPlayWhenReady(true);
        ExtractorMediaSource extractorMediaSource=new ExtractorMediaSource.Factory(defaultDataSourceFactory).createMediaSource(RawResourceDataSource.buildRawResourceUri(R.raw.video));
        simpleExoPlayer.prepare(extractorMediaSource);
        playerView.setPlayer(simpleExoPlayer);
    
        super.onStart();
    }
    
    @Override
    protected void onStop() {
        playerView.setPlayer(null);
        simpleExoPlayer.release();
        simpleExoPlayer=null;
        super.onStop();
    }
    

    }

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-04 04:27

    For those who want to play a video from assets using ExoPlayer 2 here is a 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);
    
    0 讨论(0)
  • 2021-02-04 04:29

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

    0 讨论(0)
  • 2021-02-04 04:31

    This worked for me.Try these steps:

    Get path of the file and start the player

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

    Initializing player

    private void initializePlayer(String videoUrl) {
        player = ExoPlayerFactory.newSimpleInstance(
                new DefaultRenderersFactory(getActivity()),
                new DefaultTrackSelector(), new DefaultLoadControl());
    
        playerView.setPlayer(player);
    
        player.setPlayWhenReady(playWhenReady);
        player.seekTo(currentWindow, playbackPosition);
    
        Uri uri = Uri.parse(videoUrl);
        MediaSource mediaSource = buildMediaSource(uri);
        player.prepare(mediaSource, resetPositionBoolean, false);
    }   
    

    Building media source

      private MediaSource buildMediaSource(Uri uri) {
        return new ExtractorMediaSource.Factory(
                new DefaultDataSourceFactory(getActivity(),"Exoplayer-local")).
                createMediaSource(uri);
    }
    
    0 讨论(0)
  • 2021-02-04 04:31

    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),
    };
    
    0 讨论(0)
提交回复
热议问题