Fix ChromeCast crash and bugs
This commit is contained in:
parent
a0c745641a
commit
7195ab2afd
8 changed files with 43 additions and 63 deletions
|
@ -4,6 +4,7 @@ import androidx.room.Room
|
||||||
import androidx.room.RoomDatabase
|
import androidx.room.RoomDatabase
|
||||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||||
import code.name.monkey.retromusic.auto.AutoMusicProvider
|
import code.name.monkey.retromusic.auto.AutoMusicProvider
|
||||||
|
import code.name.monkey.retromusic.cast.RetroWebServer
|
||||||
import code.name.monkey.retromusic.db.BlackListStoreDao
|
import code.name.monkey.retromusic.db.BlackListStoreDao
|
||||||
import code.name.monkey.retromusic.db.BlackListStoreEntity
|
import code.name.monkey.retromusic.db.BlackListStoreEntity
|
||||||
import code.name.monkey.retromusic.db.PlaylistWithSongs
|
import code.name.monkey.retromusic.db.PlaylistWithSongs
|
||||||
|
@ -103,6 +104,9 @@ private val mainModule = module {
|
||||||
single {
|
single {
|
||||||
androidContext().contentResolver
|
androidContext().contentResolver
|
||||||
}
|
}
|
||||||
|
single {
|
||||||
|
RetroWebServer(get())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
private val dataModule = module {
|
private val dataModule = module {
|
||||||
single {
|
single {
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package code.name.monkey.retromusic.activities.base
|
package code.name.monkey.retromusic.activities.base
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.ViewStub
|
|
||||||
import code.name.monkey.retromusic.R
|
|
||||||
import code.name.monkey.retromusic.cast.CastHelper
|
import code.name.monkey.retromusic.cast.CastHelper
|
||||||
import code.name.monkey.retromusic.cast.RetroSessionManagerListener
|
import code.name.monkey.retromusic.cast.RetroSessionManagerListener
|
||||||
import code.name.monkey.retromusic.cast.RetroWebServer
|
import code.name.monkey.retromusic.cast.RetroWebServer
|
||||||
|
@ -12,36 +10,40 @@ import com.google.android.gms.cast.framework.CastSession
|
||||||
import com.google.android.gms.cast.framework.SessionManager
|
import com.google.android.gms.cast.framework.SessionManager
|
||||||
import com.google.android.gms.common.ConnectionResult
|
import com.google.android.gms.common.ConnectionResult
|
||||||
import com.google.android.gms.common.GoogleApiAvailability
|
import com.google.android.gms.common.GoogleApiAvailability
|
||||||
|
import org.koin.android.ext.android.inject
|
||||||
|
|
||||||
|
|
||||||
abstract class AbsCastActivity : AbsSlidingMusicPanelActivity() {
|
abstract class AbsCastActivity : AbsSlidingMusicPanelActivity() {
|
||||||
|
|
||||||
private var mCastSession: CastSession? = null
|
private var mCastSession: CastSession? = null
|
||||||
private lateinit var sessionManager: SessionManager
|
private lateinit var sessionManager: SessionManager
|
||||||
private var webServer: RetroWebServer? = null
|
private val webServer: RetroWebServer by inject()
|
||||||
|
|
||||||
private var playServicesAvailable: Boolean = false
|
private var playServicesAvailable: Boolean = false
|
||||||
|
|
||||||
private val sessionManagerListener by lazy {
|
private val sessionManagerListener by lazy {
|
||||||
object : RetroSessionManagerListener {
|
object : RetroSessionManagerListener {
|
||||||
override fun onSessionStarting(castSession: CastSession) {
|
override fun onSessionStarting(castSession: CastSession) {
|
||||||
invalidateOptionsMenu()
|
webServer.start()
|
||||||
webServer = RetroWebServer.getInstance(this@AbsCastActivity)
|
|
||||||
webServer?.start()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSessionStarted(castSession: CastSession, p1: String) {
|
override fun onSessionStarted(castSession: CastSession, p1: String) {
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
mCastSession = castSession
|
mCastSession = castSession
|
||||||
loadCastQueue(MusicPlayerRemote.position)
|
loadCastQueue()
|
||||||
inflateCastController()
|
|
||||||
MusicPlayerRemote.isCasting = true
|
MusicPlayerRemote.isCasting = true
|
||||||
setAllowDragging(false)
|
setAllowDragging(false)
|
||||||
collapsePanel()
|
collapsePanel()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSessionEnding(p0: CastSession) {
|
override fun onSessionEnding(castSession: CastSession) {
|
||||||
invalidateOptionsMenu()
|
MusicPlayerRemote.isCasting = false
|
||||||
webServer?.stop()
|
castSession.remoteMediaClient?.let {
|
||||||
|
val position = it.mediaQueue.indexOfItemWithId(it.currentItem?.itemId ?: 0)
|
||||||
|
val progress = it.approximateStreamPosition
|
||||||
|
MusicPlayerRemote.position = position
|
||||||
|
MusicPlayerRemote.seekTo(progress.toInt())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSessionEnded(castSession: CastSession, p1: Int) {
|
override fun onSessionEnded(castSession: CastSession, p1: Int) {
|
||||||
|
@ -49,15 +51,18 @@ abstract class AbsCastActivity : AbsSlidingMusicPanelActivity() {
|
||||||
if (mCastSession == castSession) {
|
if (mCastSession == castSession) {
|
||||||
mCastSession = null
|
mCastSession = null
|
||||||
}
|
}
|
||||||
MusicPlayerRemote.isCasting = false
|
|
||||||
setAllowDragging(true)
|
setAllowDragging(true)
|
||||||
|
webServer.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSessionResumed(castSession: CastSession, p1: Boolean) {
|
override fun onSessionResumed(castSession: CastSession, p1: Boolean) {
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
mCastSession = castSession
|
mCastSession = castSession
|
||||||
loadCastQueue(MusicPlayerRemote.position)
|
webServer.start()
|
||||||
inflateCastController()
|
mCastSession?.remoteMediaClient?.let {
|
||||||
|
loadCastQueue(it.mediaQueue.indexOfItemWithId(it.currentItem?.itemId ?: 0), it.approximateStreamPosition)
|
||||||
|
}
|
||||||
|
|
||||||
MusicPlayerRemote.isCasting = true
|
MusicPlayerRemote.isCasting = true
|
||||||
setAllowDragging(false)
|
setAllowDragging(false)
|
||||||
collapsePanel()
|
collapsePanel()
|
||||||
|
@ -70,6 +75,7 @@ abstract class AbsCastActivity : AbsSlidingMusicPanelActivity() {
|
||||||
}
|
}
|
||||||
MusicPlayerRemote.isCasting = false
|
MusicPlayerRemote.isCasting = false
|
||||||
setAllowDragging(true)
|
setAllowDragging(true)
|
||||||
|
webServer.stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,6 +98,7 @@ abstract class AbsCastActivity : AbsSlidingMusicPanelActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
if (playServicesAvailable) {
|
if (playServicesAvailable) {
|
||||||
sessionManager.addSessionManagerListener(
|
sessionManager.addSessionManagerListener(
|
||||||
sessionManagerListener,
|
sessionManagerListener,
|
||||||
|
@ -101,7 +108,6 @@ abstract class AbsCastActivity : AbsSlidingMusicPanelActivity() {
|
||||||
mCastSession = sessionManager.currentCastSession
|
mCastSession = sessionManager.currentCastSession
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super.onResume()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
|
@ -115,33 +121,26 @@ abstract class AbsCastActivity : AbsSlidingMusicPanelActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun songChanged(position: Int) {
|
fun loadCastQueue(
|
||||||
loadCastQueue(position)
|
position: Int = MusicPlayerRemote.position,
|
||||||
}
|
progress: Long = MusicPlayerRemote.songProgressMillis.toLong(),
|
||||||
|
) {
|
||||||
fun loadCastQueue(position: Int) {
|
|
||||||
if (!MusicPlayerRemote.playingQueue.isNullOrEmpty()) {
|
|
||||||
mCastSession?.let {
|
mCastSession?.let {
|
||||||
|
if (!MusicPlayerRemote.playingQueue.isNullOrEmpty()) {
|
||||||
CastHelper.castQueue(
|
CastHelper.castQueue(
|
||||||
it,
|
it,
|
||||||
MusicPlayerRemote.playingQueue,
|
MusicPlayerRemote.playingQueue,
|
||||||
position,
|
position,
|
||||||
MusicPlayerRemote.songProgressMillis.toLong()
|
progress
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
mCastSession?.let { CastHelper.castSong(it, MusicPlayerRemote.currentSong) }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPlayingMetaChanged() {
|
override fun onQueueChanged() {
|
||||||
super.onPlayingMetaChanged()
|
super.onQueueChanged()
|
||||||
if (playServicesAvailable) {
|
if (playServicesAvailable) {
|
||||||
songChanged(MusicPlayerRemote.position)
|
loadCastQueue()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun inflateCastController() {
|
|
||||||
findViewById<ViewStub>(R.id.cast_stub)?.inflate()
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -79,6 +79,5 @@ object CastHelper {
|
||||||
setMetadata(musicMetadata)
|
setMetadata(musicMetadata)
|
||||||
setStreamDuration(song.duration)
|
setStreamDuration(song.duration)
|
||||||
}.build()
|
}.build()
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,19 +4,11 @@ import com.google.android.gms.cast.framework.CastSession
|
||||||
import com.google.android.gms.cast.framework.SessionManagerListener
|
import com.google.android.gms.cast.framework.SessionManagerListener
|
||||||
|
|
||||||
interface RetroSessionManagerListener : SessionManagerListener<CastSession> {
|
interface RetroSessionManagerListener : SessionManagerListener<CastSession> {
|
||||||
override fun onSessionResuming(p0: CastSession, p1: String) {
|
override fun onSessionResuming(p0: CastSession, p1: String) {}
|
||||||
|
|
||||||
}
|
override fun onSessionStartFailed(p0: CastSession, p1: Int) {}
|
||||||
|
|
||||||
override fun onSessionStartFailed(p0: CastSession, p1: Int) {
|
override fun onSessionResumeFailed(p0: CastSession, p1: Int) {}
|
||||||
|
|
||||||
}
|
override fun onSessionEnding(castSession: CastSession) {}
|
||||||
|
|
||||||
override fun onSessionResumeFailed(p0: CastSession, p1: Int) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onSessionEnding(p0: CastSession) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -17,13 +17,6 @@ class RetroWebServer(val context: Context) : NanoHTTPD(SERVER_PORT) {
|
||||||
const val PART_COVER_ART = "coverart"
|
const val PART_COVER_ART = "coverart"
|
||||||
const val PART_SONG = "song"
|
const val PART_SONG = "song"
|
||||||
const val PARAM_ID = "id"
|
const val PARAM_ID = "id"
|
||||||
private var mRetroWebServer: RetroWebServer? = null
|
|
||||||
fun getInstance(context: Context): RetroWebServer {
|
|
||||||
if (mRetroWebServer == null) {
|
|
||||||
mRetroWebServer = RetroWebServer(context)
|
|
||||||
}
|
|
||||||
return mRetroWebServer!!
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun serve(session: IHTTPSession?): Response {
|
override fun serve(session: IHTTPSession?): Response {
|
||||||
|
|
|
@ -49,7 +49,6 @@ object MusicPlayerRemote : KoinComponent {
|
||||||
if (value) {
|
if (value) {
|
||||||
musicService?.quit()
|
musicService?.quit()
|
||||||
}
|
}
|
||||||
println(value.toString() + "" + isCasting.toString())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:id="@+id/castMiniController"
|
|
||||||
class="com.google.android.gms.cast.framework.media.widget.MiniControllerFragment"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content" />
|
|
|
@ -39,11 +39,11 @@
|
||||||
android:layout_height="@dimen/mini_player_height"
|
android:layout_height="@dimen/mini_player_height"
|
||||||
tools:layout="@layout/fragment_mini_player" />
|
tools:layout="@layout/fragment_mini_player" />
|
||||||
|
|
||||||
<ViewStub
|
<fragment
|
||||||
android:id="@+id/cast_stub"
|
android:id="@+id/castMiniController"
|
||||||
|
class="com.google.android.gms.cast.framework.media.widget.MiniControllerFragment"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content" />
|
||||||
android:layout="@layout/cast_controller_layout" />
|
|
||||||
|
|
||||||
</FrameLayout>
|
</FrameLayout>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue