Created
March 8, 2018 22:32
-
-
Save nazmulidris/6a40763185ed0653320d8efae66256bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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