ExoPlayer - play local mp4 file in SD card

后端 未结 8 610
囚心锁ツ
囚心锁ツ 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条回答
  •  萌比男神i
    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();
    }
    

    }

提交回复
热议问题