Skip to content

Instantly share code, notes, and snippets.

@nazmulidris
Created March 8, 2018 22:32
Show Gist options
  • Select an option

  • Save nazmulidris/6a40763185ed0653320d8efae66256bc to your computer and use it in GitHub Desktop.

Select an option

Save nazmulidris/6a40763185ed0653320d8efae66256bc to your computer and use it in GitHub Desktop.
enum class SourceType {
local_audio, local_video, http_audio, http_video, playlist;
}
data class PlayerState(var window: Int = 0,
var position: Long = 0,
var whenReady: Boolean = true,
var source: SourceType = local_audio)
class PlayerHolder(val context: Context,
val playerView: PlayerView,
val playerState: PlayerState) : AnkoLogger {
val player: ExoPlayer
init {
// Create the player instance.
player = ExoPlayerFactory.newSimpleInstance(ctx, DefaultTrackSelector())
.also {
playerView.player = it // Bind to the view.
info { "SimpleExoPlayer created" }
}
}
fun start() {
// Load media.
prepare(buildMediaSource(selectMediaToPlay(state.source)))
// Start playback when media has buffered enough.
playWhenReady = true
}
fun selectMediaToPlay(source: Source): Uri {
return when (source) {
Source.local_audio -> Uri.parse("asset:///audio/file.mp3")
Source.local_video -> Uri.parse("asset:///video/file.mp4")
Source.http_audio -> Uri.parse("http://site.../file.mp3")
Source.http_video -> Uri.parse("http://site.../file.mp4")
}
}
private fun buildMediaSource(uri: Uri): ExtractorMediaSource {
return ExtractorMediaSource.Factory(
DefaultDataSourceFactory(ctx, "videoapp")).createMediaSource(uri)
}
fun stop() { ... }
fun release() { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment