Skip to content

Commit

Permalink
Move MediaRouter into Service
Browse files Browse the repository at this point in the history
  • Loading branch information
dturner committed Oct 1, 2020
1 parent 9a237bf commit 9b7a9f8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
27 changes: 0 additions & 27 deletions app/src/main/java/com/example/android/uamp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ class MainActivity : AppCompatActivity() {
InjectorUtils.provideMainActivityViewModel(this)
}
private var castContext: CastContext? = null
private lateinit var selector: MediaRouteSelector
private lateinit var router: MediaRouter


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -56,16 +53,6 @@ class MainActivity : AppCompatActivity() {

setContentView(R.layout.activity_main)

router = MediaRouter.getInstance(this)
selector = MediaRouteSelector.Builder()
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build()
router.routerParams = MediaRouterParams.Builder().setTransferToLocalEnabled(true).build()
router.addCallback(
selector, MediaRouterCallback(),
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY
)

// Since UAMP is a music player, the volume controls should adjust the music volume while
// in the app.
volumeControlStream = AudioManager.STREAM_MUSIC
Expand Down Expand Up @@ -133,20 +120,6 @@ class MainActivity : AppCompatActivity() {
private fun getBrowseFragment(mediaId: String): MediaItemFragment? {
return supportFragmentManager.findFragmentByTag(mediaId) as MediaItemFragment?
}

private inner class MediaRouterCallback : MediaRouter.Callback() {
override fun onRouteSelected(
router: MediaRouter,
route: MediaRouter.RouteInfo,
reason: Int
) {
if (reason == MediaRouter.UNSELECT_REASON_ROUTE_CHANGED) {
Log.d(TAG, "Unselected because route changed, continue playback")
} else if (reason == MediaRouter.UNSELECT_REASON_STOPPED) {
Log.d(TAG, "Unselected because route was stopped, stop playback")
}
}
}
}

private const val TAG = "MainActivity"
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.media.MediaBrowserServiceCompat
import androidx.media.MediaBrowserServiceCompat.BrowserRoot.EXTRA_RECENT
import androidx.mediarouter.media.MediaControlIntent
import androidx.mediarouter.media.MediaRouteSelector
import androidx.mediarouter.media.MediaRouter
import androidx.mediarouter.media.MediaRouterParams
import com.example.android.uamp.media.extensions.album
import com.example.android.uamp.media.extensions.flag
import com.example.android.uamp.media.extensions.id
Expand Down Expand Up @@ -104,6 +108,10 @@ open class MusicService : MediaBrowserServiceCompat() {

private lateinit var storage: PersistentStorage

private lateinit var mediaRouteSelector: MediaRouteSelector
private lateinit var mediaRouter: MediaRouter
private val mediaRouterCallback = MediaRouterCallback()

/**
* This must be `by lazy` because the source won't initially be ready.
* See [MusicService.onLoadChildren] to see where it's accessed (and first
Expand Down Expand Up @@ -216,6 +224,17 @@ open class MusicService : MediaBrowserServiceCompat() {
packageValidator = PackageValidator(this, R.xml.allowed_media_browser_callers)

storage = PersistentStorage.getInstance(applicationContext)

mediaRouter = MediaRouter.getInstance(this)
mediaRouteSelector = MediaRouteSelector.Builder()
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build()
mediaRouter.routerParams =
MediaRouterParams.Builder().setTransferToLocalEnabled(true).build()
mediaRouter.addCallback(
mediaRouteSelector, mediaRouterCallback,
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY
)
}

/**
Expand Down Expand Up @@ -250,6 +269,9 @@ open class MusicService : MediaBrowserServiceCompat() {
// Free ExoPlayer resources.
exoPlayer.removeListener(playerListener)
exoPlayer.release()

// Stop listening for route changes.
mediaRouter.removeCallback(mediaRouterCallback)
}

/**
Expand Down Expand Up @@ -500,7 +522,10 @@ open class MusicService : MediaBrowserServiceCompat() {
} else {

val playbackStartPositionMs =
extras?.getLong(MEDIA_DESCRIPTION_EXTRAS_START_PLAYBACK_POSITION_MS, C.TIME_UNSET)
extras?.getLong(
MEDIA_DESCRIPTION_EXTRAS_START_PLAYBACK_POSITION_MS,
C.TIME_UNSET
)
?: C.TIME_UNSET

preparePlaylist(
Expand Down Expand Up @@ -650,6 +675,21 @@ open class MusicService : MediaBrowserServiceCompat() {
).show()
}
}

inner class MediaRouterCallback : MediaRouter.Callback() {
override fun onRouteSelected(
router: MediaRouter,
route: MediaRouter.RouteInfo,
reason: Int
) {
if (reason == MediaRouter.UNSELECT_REASON_ROUTE_CHANGED) {
Log.d(TAG, "Unselected because route changed, continue playback")
} else if (reason == MediaRouter.UNSELECT_REASON_STOPPED) {
Log.d(TAG, "Unselected because route was stopped, stop playback")
currentPlayer.stop()
}
}
}
}

/*
Expand Down

0 comments on commit 9b7a9f8

Please sign in to comment.