Skip to content

Commit

Permalink
Added start option
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafal committed Apr 30, 2018
1 parent 2245d78 commit eb8aa90
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MediaStateResolver(private val player: MediaPlayer) : MediaPlayer.OnPrepar
private val bufferSubject: Subject<Percent> = BehaviorSubject.create()
private val positionSubject: Subject<Second> = BehaviorSubject.create()
private val durationSubject: Subject<Second> = BehaviorSubject.create()
private var preapred = false

private val infoFunction4 = Function4 { t1: MediaState, t2: Percent, t3: Second, t4: Second -> MediaInfo(t1, t2, t3, t3 / 1000, t4) }
val infoSubject: Observable<MediaInfo> = Observable.combineLatest(
Expand Down Expand Up @@ -91,7 +92,9 @@ class MediaStateResolver(private val player: MediaPlayer) : MediaPlayer.OnPrepar
}

fun postWhenPrepared(runnable: MediaRunnable) {
onPreparedRunnables.add(runnable)
if (preapred) runnable.invoke(player)
else
onPreparedRunnables.add(runnable)
}

internal fun startPlayer(mp: MediaPlayer) {
Expand Down Expand Up @@ -134,12 +137,18 @@ class MediaStateResolver(private val player: MediaPlayer) : MediaPlayer.OnPrepar

fun stop() {
if (player.isPlaying) {
println("STOPPING")
player.pause()
positionSubject.onNext(0)
stateSubject.onNext(MediaState.STOPPED)
}
}
fun start() {
if (!player.isPlaying) {
postWhenPrepared {
startPlayer(it)
}
}
}

fun seekTo(millisecond: Millisecond) {
if (player.isPlaying)
Expand All @@ -152,4 +161,6 @@ class MediaStateResolver(private val player: MediaPlayer) : MediaPlayer.OnPrepar
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PlayerService : LifecycleService() {
is ServiceCommand.Resume -> mediaManager.resume()
is ServiceCommand.SeekTo -> mediaManager.seekTo(it.millisecond)
is ServiceCommand.Restart -> mediaManager.restart()
is ServiceCommand.Start -> mediaManager.start()

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sealed class ServiceCommand {
object Resume : ServiceCommand()
object Finish : ServiceCommand()
object Restart : ServiceCommand()
object Start : ServiceCommand()
class SeekTo(val millisecond: Millisecond) : ServiceCommand()
class LoadStreamMusic(val url: String, val attr: AudioAttributes?) : ServiceCommand()
class LoadResourceMusic(val resourceId: Int, val attr: AudioAttributes?) : ServiceCommand()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ open class InternalMediaPlayer(val player: MediaPlayer, val context: Context,
}

override fun seekTo(millisecond: Millisecond) {

resolver.seekTo(millisecond)
}

Expand All @@ -97,6 +96,10 @@ open class InternalMediaPlayer(val player: MediaPlayer, val context: Context,
resolver.stop()
}

override fun start() {
resolver.start()
}

override fun finish() {
resolver.finalize()
mediaDisposable?.dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface MediaManager {
fun seekTo(millisecond: Millisecond)
fun finish()
fun pause()
fun start()
fun resume()
fun stop()
fun restart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,9 @@ object MediaServiceCommandEmitter : MediaManager {
commandSubject.onNext(ServiceCommand.Restart)
}

override fun start() {
commandSubject.onNext(ServiceCommand.Start)
}


}

0 comments on commit eb8aa90

Please sign in to comment.