From 9420f86be6d2302158c8361ba6757f23d60fb55a Mon Sep 17 00:00:00 2001 From: kshivang Date: Tue, 8 Oct 2019 21:08:02 +0530 Subject: [PATCH] autoplay api improved --- .../rever/goonjexample/AudioPlayerActivity.kt | 6 ++--- goonj/src/main/java/ai/rever/goonj/Goonj.kt | 17 ++++++++++--- .../ai/rever/goonj/interfaces/AudioPlayer.kt | 4 +-- .../ai/rever/goonj/interfaces/GoonjPlayer.kt | 25 +++++++++++++------ .../rever/goonj/manager/GoonjPlayerManager.kt | 10 +++++--- .../ai/rever/goonj/player/LocalAudioPlayer.kt | 24 ++++++++++-------- .../rever/goonj/player/RemoteAudioPlayer.kt | 25 +++++++++++-------- 7 files changed, 72 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/ai/rever/goonjexample/AudioPlayerActivity.kt b/app/src/main/java/ai/rever/goonjexample/AudioPlayerActivity.kt index d5624bf..08a883c 100644 --- a/app/src/main/java/ai/rever/goonjexample/AudioPlayerActivity.kt +++ b/app/src/main/java/ai/rever/goonjexample/AudioPlayerActivity.kt @@ -45,7 +45,7 @@ class AudioPlayerActivity : AppCompatActivity(), GoonjPlayer { audioPlayerPlayPauseToggleBtn.isChecked = it != GoonjPlayerState.PLAYING }?.addTo(compositeDisposable) - currentPlayingTrackObservable?.subscribe(::onPlayingTrackChange) + currentTrackObservable?.subscribe(::onPlayingTrackChange) ?.addTo(compositeDisposable) } @@ -98,8 +98,8 @@ class AudioPlayerActivity : AppCompatActivity(), GoonjPlayer { seekTo(trackPosition - 3000) } - audioPlayerAutoplaySwitch.setOnCheckedChangeListener { _, autoplay -> - setAutoplay(autoplay) + audioPlayerAutoplaySwitch.setOnCheckedChangeListener { _, currentState -> + autoplay = currentState } audioPlayerSkipNext.setOnClickListener { diff --git a/goonj/src/main/java/ai/rever/goonj/Goonj.kt b/goonj/src/main/java/ai/rever/goonj/Goonj.kt index f196d8d..457c7e0 100644 --- a/goonj/src/main/java/ai/rever/goonj/Goonj.kt +++ b/goonj/src/main/java/ai/rever/goonj/Goonj.kt @@ -152,7 +152,6 @@ object Goonj { ) } - fun setAutoplay(autoplay : Boolean) = run { GoonjPlayerManager.setAutoplay(autoplay) } fun removeTrack(index : Int) = run { GoonjPlayerManager.removeTrack(index) } @@ -168,15 +167,27 @@ object Goonj { fun finishTrack() = run { GoonjPlayerManager.finishTrack() } + + var autoplay: Boolean + get() = GoonjPlayerManager.autoplay + set(value) { + run { + GoonjPlayerManager.autoplay = value + } + } + + val playerState: GoonjPlayerState? get() = GoonjPlayerManager.playerStateBehaviorSubject.value + + val currentTrack: Track? get() = GoonjPlayerManager.currentTrackSubject.value + val playerStateObservable: Observable? get() = GoonjPlayerManager.playerStateBehaviorSubject.observeOn(AndroidSchedulers.mainThread()) - val currentPlayingTrack: Observable? get() = GoonjPlayerManager.currentPlayingTrack.observeOn(AndroidSchedulers.mainThread()) + val currentTrackObservable: Observable? get() = GoonjPlayerManager.currentTrackSubject.observeOn(AndroidSchedulers.mainThread()) val trackList get() = GoonjPlayerManager.trackList val trackPosition get() = GoonjPlayerManager.trackPosition - // internal method internal fun startForeground(notificationId: Int, notification: Notification?) = run { startForeground(notificationId, notification) diff --git a/goonj/src/main/java/ai/rever/goonj/interfaces/AudioPlayer.kt b/goonj/src/main/java/ai/rever/goonj/interfaces/AudioPlayer.kt index a37d597..8e28b5d 100644 --- a/goonj/src/main/java/ai/rever/goonj/interfaces/AudioPlayer.kt +++ b/goonj/src/main/java/ai/rever/goonj/interfaces/AudioPlayer.kt @@ -17,11 +17,11 @@ interface AudioPlayer { fun suspend() fun unsuspend() - fun setAutoplay(autoplay: Boolean) + var autoplay: Boolean fun seekTo(index: Int, positionMs: Long) {} - fun getTrackPosition(): Long = GoonjPlayerManager.currentPlayingTrack.value?.trackState?.position?: 0 + fun getTrackPosition(): Long = GoonjPlayerManager.currentTrackSubject.value?.trackState?.position?: 0 fun connect(route: MediaRouter.RouteInfo) {} fun startNewSession() {} diff --git a/goonj/src/main/java/ai/rever/goonj/interfaces/GoonjPlayer.kt b/goonj/src/main/java/ai/rever/goonj/interfaces/GoonjPlayer.kt index d7417e8..27ce663 100644 --- a/goonj/src/main/java/ai/rever/goonj/interfaces/GoonjPlayer.kt +++ b/goonj/src/main/java/ai/rever/goonj/interfaces/GoonjPlayer.kt @@ -1,8 +1,9 @@ package ai.rever.goonj.interfaces import ai.rever.goonj.Goonj +import ai.rever.goonj.GoonjPlayerState +import ai.rever.goonj.manager.GoonjPlayerManager import ai.rever.goonj.models.Track -import io.reactivex.android.schedulers.AndroidSchedulers /** * This interface will be visible to User @@ -25,9 +26,13 @@ interface GoonjPlayer { Goonj.seekTo(positionMS) } - fun setAutoplay(autoplay : Boolean) { - Goonj.setAutoplay(autoplay) - } + + var autoplay: Boolean + get() = Goonj.autoplay + set(value) { + Goonj.autoplay = value + } + fun addTrack(track: Track, index: Int ?= null) { Goonj.addTrack(track, index) @@ -47,7 +52,6 @@ interface GoonjPlayer { } - val trackList get() = Goonj.trackList fun removeTrack(index : Int){ Goonj.removeTrack(index) @@ -65,9 +69,16 @@ interface GoonjPlayer { fun finishTrack() = Goonj.finishTrack() - val trackPosition get() = Goonj.trackPosition + val playerState: GoonjPlayerState? get() = Goonj.playerState + + val currentTrack: Track? get() = Goonj.currentTrack val playerStateObservable get() = Goonj.playerStateObservable - val currentPlayingTrackObservable get() = Goonj.currentPlayingTrack + val currentTrackObservable get() = Goonj.currentTrackObservable + + val trackList get() = Goonj.trackList + + val trackPosition get() = Goonj.trackPosition + } \ No newline at end of file diff --git a/goonj/src/main/java/ai/rever/goonj/manager/GoonjPlayerManager.kt b/goonj/src/main/java/ai/rever/goonj/manager/GoonjPlayerManager.kt index cb62fa7..58d19d0 100644 --- a/goonj/src/main/java/ai/rever/goonj/manager/GoonjPlayerManager.kt +++ b/goonj/src/main/java/ai/rever/goonj/manager/GoonjPlayerManager.kt @@ -27,7 +27,7 @@ internal object GoonjPlayerManager { } internal val playerStateBehaviorSubject: BehaviorSubject = BehaviorSubject.create() - internal val currentPlayingTrack: BehaviorSubject = BehaviorSubject.create() + internal val currentTrackSubject: BehaviorSubject = BehaviorSubject.create() private val player: AudioPlayer? get() { // isRemote = mediaRoute?.supportsControlCategory( @@ -68,7 +68,11 @@ internal object GoonjPlayerManager { player?.startNewSession() } - internal fun setAutoplay(autoplay : Boolean) = player?.setAutoplay(autoplay) + internal var autoplay: Boolean + get() = player?.autoplay ?: false + set(value) { + player?.autoplay = value + } internal fun removeTrack(index : Int){ mTrackList.removeAt(index) @@ -108,7 +112,7 @@ internal object GoonjPlayerManager { internal fun finishTrack(){ pause() removeNotification() - onTrackComplete(currentPlayingTrack.value ?: return) + onTrackComplete(currentTrackSubject.value ?: return) } internal fun onTrackComplete(track: Track) { diff --git a/goonj/src/main/java/ai/rever/goonj/player/LocalAudioPlayer.kt b/goonj/src/main/java/ai/rever/goonj/player/LocalAudioPlayer.kt index 8481768..596ab1e 100644 --- a/goonj/src/main/java/ai/rever/goonj/player/LocalAudioPlayer.kt +++ b/goonj/src/main/java/ai/rever/goonj/player/LocalAudioPlayer.kt @@ -51,7 +51,7 @@ class LocalAudioPlayer: AudioPlayer { get() = Observable.interval(1000, TimeUnit.MILLISECONDS) .takeWhile { !isSuspended && GoonjPlayerManager.playerStateBehaviorSubject.value == GoonjPlayerState.PLAYING } - .map { (GoonjPlayerManager.currentPlayingTrack.value?.trackState?.position?: 0) + 1000 } + .map { (GoonjPlayerManager.currentTrackSubject.value?.trackState?.position?: 0) + 1000 } private val compositeDisposable = CompositeDisposable() private var timerDisposable: Disposable? = null @@ -74,7 +74,8 @@ class LocalAudioPlayer: AudioPlayer { private val concatenatingMediaSource by lazy { ConcatenatingMediaSource() } - private var autoplay : Boolean = true + private var _autoplay = false + private val trackList get() = GoonjPlayerManager.trackList private fun onStart() { @@ -107,9 +108,9 @@ class LocalAudioPlayer: AudioPlayer { private fun onTrackPositionChange(position: Long = getTrackPosition()) { - GoonjPlayerManager.currentPlayingTrack.value?.let { track -> + GoonjPlayerManager.currentTrackSubject.value?.let { track -> track.trackState.position = position - GoonjPlayerManager.currentPlayingTrack.onNext(track) + GoonjPlayerManager.currentTrackSubject.onNext(track) } } @@ -144,7 +145,7 @@ class LocalAudioPlayer: AudioPlayer { if (trackList.isEmpty()) return player?.apply { val currentTrack = trackList[currentWindowIndex] - val lastKnownTrack = GoonjPlayerManager.currentPlayingTrack.value + val lastKnownTrack = GoonjPlayerManager.currentTrackSubject.value if (contentDuration > 0) { currentTrack.trackState.duration = contentDuration @@ -156,7 +157,7 @@ class LocalAudioPlayer: AudioPlayer { currentTrack.trackState.position = 0 } - GoonjPlayerManager.currentPlayingTrack.onNext(currentTrack) + GoonjPlayerManager.currentTrackSubject.onNext(currentTrack) if (currentTrack.id != lastKnownTrack?.id) { GoonjPlayerManager.onTrackComplete(lastKnownTrack ?: return) @@ -208,7 +209,7 @@ class LocalAudioPlayer: AudioPlayer { override fun unsuspend() { isSuspended = false - GoonjPlayerManager.currentPlayingTrack.value?.trackState?.apply { + GoonjPlayerManager.currentTrackSubject.value?.trackState?.apply { seekTo(index, position) if (state == GoonjPlayerState.PLAYING) { resume() @@ -268,9 +269,12 @@ class LocalAudioPlayer: AudioPlayer { override fun getTrackPosition() = player?.currentPosition ?: 0 - override fun setAutoplay(autoplay: Boolean) { - this.autoplay = autoplay - } + override var autoplay: Boolean + get() = _autoplay + set(value) { + _autoplay = value + } + override fun onRemoveNotification() { playerNotificationManager.setPlayer(null) diff --git a/goonj/src/main/java/ai/rever/goonj/player/RemoteAudioPlayer.kt b/goonj/src/main/java/ai/rever/goonj/player/RemoteAudioPlayer.kt index e2e8423..bfc3728 100644 --- a/goonj/src/main/java/ai/rever/goonj/player/RemoteAudioPlayer.kt +++ b/goonj/src/main/java/ai/rever/goonj/player/RemoteAudioPlayer.kt @@ -23,7 +23,9 @@ class RemoteAudioPlayer: AudioPlayer { private var player: RemotePlaybackClient? = null - private var autoplay : Boolean = true + private var _autoplay = false + + private var isHandlerRunning = false override fun connect(route: MediaRouter.RouteInfo) { @@ -64,7 +66,7 @@ class RemoteAudioPlayer: AudioPlayer { setMediaLoadRequest(it) } - GoonjPlayerManager.currentPlayingTrack.onNext(track) + GoonjPlayerManager.currentTrackSubject.onNext(track) setStatus(itemStatus, defaultState = GoonjPlayerState.PAUSED) }) } @@ -89,7 +91,7 @@ class RemoteAudioPlayer: AudioPlayer { } fun getStatus(seek: Boolean, positionMs: Long = 0) { - val track = GoonjPlayerManager.currentPlayingTrack.value + val track = GoonjPlayerManager.currentTrackSubject.value if (player?.hasSession() != true || track?.trackState?.remoteItemId == null) { // if trackList is not valid or track id not assigend yet. // just return, it's not fatal @@ -128,7 +130,7 @@ class RemoteAudioPlayer: AudioPlayer { } override fun unsuspend() { - GoonjPlayerManager.currentPlayingTrack.value?.let { + GoonjPlayerManager.currentTrackSubject.value?.let { play(it) } } @@ -170,12 +172,13 @@ class RemoteAudioPlayer: AudioPlayer { } + override var autoplay + get() = _autoplay + set(value) { + _autoplay = value - override fun setAutoplay(autoplay: Boolean) { - this.autoplay = autoplay - - // TODO : Re-create RemoteAudioPlayer with playlist support - } + // TODO : Re-create RemoteAudioPlayer with playlist support + } private fun seekInternal(item: Track) { if (player?.hasSession() != true) { @@ -196,7 +199,7 @@ class RemoteAudioPlayer: AudioPlayer { map ) - val track = GoonjPlayerManager.currentPlayingTrack.value?: return + val track = GoonjPlayerManager.currentTrackSubject.value?: return if (itemStatus?.apply { track.trackState.state = when (playbackState) { PLAYBACK_STATE_BUFFERING -> GoonjPlayerState.BUFFERING @@ -239,7 +242,7 @@ class RemoteAudioPlayer: AudioPlayer { } private fun updateTrackPosition(itemId: String?, itemStatus: MediaItemStatus?){ - GoonjPlayerManager.currentPlayingTrack.value?.let { track -> + GoonjPlayerManager.currentTrackSubject.value?.let { track -> if (track.id == itemId) { itemStatus?.contentPosition?.let { track.trackState.position = it