Skip to content

Commit

Permalink
modify the controller of setting the state of player.
Browse files Browse the repository at this point in the history
  • Loading branch information
weiancheng committed Dec 21, 2017
1 parent 6360808 commit a182fa4
Showing 1 changed file with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ class ExoPlayerWrapper(context: Context): IMusicPlayer {

private var listener: PlayerEventListenerImpl ?= null


// listeners
private var durationListener: (duration: Int) -> Unit = {}
private var bufferPercentage: (percent: Int) -> Unit = {}
private var currentTime: (time: Int) -> Unit = {}

init {
this.context = context
}
Expand All @@ -63,49 +57,49 @@ class ExoPlayerWrapper(context: Context): IMusicPlayer {

initExoPlayer(uri)
exoPlayer.playWhenReady = true
playerState = Play
setPlayerState(Play)
}

override fun play() {
when (isPlaying) {
true -> {
timer.pause()
playerState = Pause
setPlayerState(Pause)
}

false -> {
timer.resume()
playerState = Play
setPlayerState(Play)
}
}

if (isPlaying) {
timer.pause()
playerState = Pause
setPlayerState(Pause)
} else {
timer.resume()
playerState = Play
setPlayerState(Play)
}
exoPlayer.playWhenReady = !isPlaying
}

override fun stop() {
exoPlayer.playWhenReady = false
exoPlayer.release()
exoPlayer.stop()
timer.stop()
playerState = Standby
setPlayerState(Standby)
}

override fun pause() {
exoPlayer.playWhenReady = false
timer.pause()
playerState = Pause
setPlayerState(Pause)
}

override fun resume() {
exoPlayer.playWhenReady = true
timer.resume()
playerState = Play
setPlayerState(Play)
}

override fun setRepeat(isRepeat: Boolean) {
Expand All @@ -129,8 +123,11 @@ class ExoPlayerWrapper(context: Context): IMusicPlayer {
this.listener = listener
}

fun setCallBack(listener: PlayerEventListenerImpl) {
this.listener = listener
private fun setPlayerState(state: MusicPlayerState) {
if (state != playerState) {
playerState = state
listener?.onPlayerStateChanged(state)
}
}

private fun initExoPlayer(url: String) {
Expand Down Expand Up @@ -171,12 +168,12 @@ class ExoPlayerWrapper(context: Context): IMusicPlayer {
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
musicPlayer.isPlaying = playWhenReady
if (playbackState == STATE_ENDED) {
musicPlayer.playerState = Standby
musicPlayer.setPlayerState(Standby)
}
}

override fun onLoadingChanged(isLoading: Boolean) {
musicPlayer.bufferPercentage(exoPlayer.bufferedPercentage)
musicPlayer.listener?.onBufferPercentage(exoPlayer.bufferedPercentage)
}

override fun onPositionDiscontinuity() {
Expand All @@ -189,16 +186,15 @@ class ExoPlayerWrapper(context: Context): IMusicPlayer {

override fun onTimelineChanged(timeline: Timeline?, manifest: Any?) {
if (exoPlayer.duration > 0) {
musicPlayer.durationListener(exoPlayer.duration.div(1000).toInt())
musicPlayer.listener?.onDurationChanged(exoPlayer.duration.div(1000).toInt())
}

musicPlayer.timer = PausableTimer(exoPlayer.duration, 1)
musicPlayer.timer = PausableTimer(exoPlayer.duration, 1000)
musicPlayer.timer.onTick = { millisUntilFinished ->
musicPlayer.currentTime(millisUntilFinished.div(1000).toInt())
musicPlayer.listener?.onCurrentTime(millisUntilFinished.div(1000).toInt())
}
musicPlayer.timer.onFinish = {
musicPlayer.currentTime(exoPlayer.duration.div(1000).toInt())
musicPlayer.listener?.onCurrentTime(exoPlayer.duration.div(1000).toInt())
}
musicPlayer.timer.start()
}
Expand Down

0 comments on commit a182fa4

Please sign in to comment.