Skip to content

Instantly share code, notes, and snippets.

@mikkipastel
Last active July 6, 2020 12:07
Show Gist options
  • Save mikkipastel/ba8e261d5d202cb3ac192011dd17f5f1 to your computer and use it in GitHub Desktop.
Save mikkipastel/ba8e261d5d202cb3ac192011dd17f5f1 to your computer and use it in GitHub Desktop.
private fun initializePlayer() {
if (player == null) {
player = ExoPlayerFactory.newSimpleInstance(
DefaultRenderersFactory(context),
DefaultTrackSelector(),
DefaultLoadControl())
playerView?.setPlayer(player)
player.setPlayWhenReady(playWhenReady)
player.seekTo(currentWindow, playbackPosition)
}
val mediaSource = buildMediaSource(Uri.parse(getString(R.string.media_url_mp4)))
player.prepare(mediaSource, true, false)
}
private fun buildMediaSource(uri: Uri): MediaSource {
val userAgent = "exoplayer-codelab"
if (uri.getLastPathSegment().contains("mp3") || uri.getLastPathSegment().contains("mp4")) {
return ExtractorMediaSource.Factory(DefaultHttpDataSourceFactory(userAgent))
.createMediaSource(uri)
} else if (uri.getLastPathSegment().contains("m3u8")) {
return HlsMediaSource.Factory(DefaultHttpDataSourceFactory(userAgent))
.createMediaSource(uri)
} else {
val dashChunkSourceFactory = DefaultDashChunkSource.Factory(
DefaultHttpDataSourceFactory("ua", BANDWIDTH_METER))
val manifestDataSourceFactory = DefaultHttpDataSourceFactory(userAgent)
return DashMediaSource.Factory(dashChunkSourceFactory, manifestDataSourceFactory).createMediaSource(uri)
}
}
private fun releasePlayer() {
if (player != null) {
playbackPosition = player.getCurrentPosition()
currentWindow = player.getCurrentWindowIndex()
playWhenReady = player.getPlayWhenReady()
player.release()
player = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment