Skip to content

Commit

Permalink
autoplay api improved
Browse files Browse the repository at this point in the history
  • Loading branch information
kshivang committed Oct 8, 2019
1 parent 381697d commit 9420f86
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class AudioPlayerActivity : AppCompatActivity(), GoonjPlayer {
audioPlayerPlayPauseToggleBtn.isChecked = it != GoonjPlayerState.PLAYING
}?.addTo(compositeDisposable)

currentPlayingTrackObservable?.subscribe(::onPlayingTrackChange)
currentTrackObservable?.subscribe(::onPlayingTrackChange)
?.addTo(compositeDisposable)
}

Expand Down Expand Up @@ -98,8 +98,8 @@ class AudioPlayerActivity : AppCompatActivity(), GoonjPlayer {
seekTo(trackPosition - 3000)
}

audioPlayerAutoplaySwitch.setOnCheckedChangeListener { _, autoplay ->
setAutoplay(autoplay)
audioPlayerAutoplaySwitch.setOnCheckedChangeListener { _, currentState ->
autoplay = currentState
}

audioPlayerSkipNext.setOnClickListener {
Expand Down
17 changes: 14 additions & 3 deletions goonj/src/main/java/ai/rever/goonj/Goonj.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ object Goonj {
)
}

fun setAutoplay(autoplay : Boolean) = run { GoonjPlayerManager.setAutoplay(autoplay) }

fun removeTrack(index : Int) = run { GoonjPlayerManager.removeTrack(index) }

Expand All @@ -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<GoonjPlayerState>? get() = GoonjPlayerManager.playerStateBehaviorSubject.observeOn(AndroidSchedulers.mainThread())

val currentPlayingTrack: Observable<Track>? get() = GoonjPlayerManager.currentPlayingTrack.observeOn(AndroidSchedulers.mainThread())
val currentTrackObservable: Observable<Track>? 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)
Expand Down
4 changes: 2 additions & 2 deletions goonj/src/main/java/ai/rever/goonj/interfaces/AudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
25 changes: 18 additions & 7 deletions goonj/src/main/java/ai/rever/goonj/interfaces/GoonjPlayer.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -47,7 +52,6 @@ interface GoonjPlayer {
}


val trackList get() = Goonj.trackList

fun removeTrack(index : Int){
Goonj.removeTrack(index)
Expand All @@ -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

}
10 changes: 7 additions & 3 deletions goonj/src/main/java/ai/rever/goonj/manager/GoonjPlayerManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal object GoonjPlayerManager {
}

internal val playerStateBehaviorSubject: BehaviorSubject<GoonjPlayerState> = BehaviorSubject.create()
internal val currentPlayingTrack: BehaviorSubject<Track> = BehaviorSubject.create()
internal val currentTrackSubject: BehaviorSubject<Track> = BehaviorSubject.create()

private val player: AudioPlayer? get() {
// isRemote = mediaRoute?.supportsControlCategory(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 14 additions & 10 deletions goonj/src/main/java/ai/rever/goonj/player/LocalAudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 14 additions & 11 deletions goonj/src/main/java/ai/rever/goonj/player/RemoteAudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -64,7 +66,7 @@ class RemoteAudioPlayer: AudioPlayer {
setMediaLoadRequest(it)
}

GoonjPlayerManager.currentPlayingTrack.onNext(track)
GoonjPlayerManager.currentTrackSubject.onNext(track)
setStatus(itemStatus, defaultState = GoonjPlayerState.PAUSED)
})
}
Expand All @@ -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
Expand Down Expand Up @@ -128,7 +130,7 @@ class RemoteAudioPlayer: AudioPlayer {
}

override fun unsuspend() {
GoonjPlayerManager.currentPlayingTrack.value?.let {
GoonjPlayerManager.currentTrackSubject.value?.let {
play(it)
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9420f86

Please sign in to comment.