0

Below is the code to How I initialize the Exoplayer.

 private void initializePlayer(String path) {
   player = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);
   player.addListener(componentListener);
   Uri uri = Uri.parse(path);
   MediaSource mediaSource = buildMediaSource(uri);
   player.prepare(mediaSource, true, false);
   playerView.setPlayer(player);
 }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ankit Lathiya
  • 199
  • 1
  • 12

1 Answers1

0

Initialize the player and pass the url to it to play the any type of video in exoplayer:

  public void initPlayer() {
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelection.Factory videoTrackSelectionFactory =
            new AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector = new
            DefaultTrackSelector(videoTrackSelectionFactory);
    player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
    VideoSource = buildMediaSource(uri);
    ExoPlayerView.setPlayer(player);
}

and build media source function is:

private MediaSource buildMediaSource(Uri uri) {
    String userAgent = Util.getUserAgent(this, getApplicationContext().getApplicationInfo().packageName);
    DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent, null, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, true);
    DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(VideoPlayer.this, null, httpDataSourceFactory);
    TextUtils.isEmpty(null);
    int type = Util.inferContentType(uri);
    switch (type) {
        case C.TYPE_SS:
            return new SsMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
        case C.TYPE_DASH:
            return new DashMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
        case C.TYPE_HLS:
            return new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
        case C.TYPE_OTHER:
            return new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
        default: {
            throw new IllegalStateException("Unsupported type: " + type);
        }
    }
}

Also call the initPlayer() in your activity oncreate function

Waqas Yousaf
  • 264
  • 4
  • 13