1

I have an android tv media app that uses exo player to play content. The app implements a media session, and the exo player media session extension so users can control the playback trough voice commands. When activating google assistant, commands like 'pause','resume','rewind','fast-forward' work perfectly with the player. Playback stops when you say pause, it goes beck if you say rewind etc. However the 'Play content name' voice command doesn't work at all. I have implemented a playbackPreparer for the media session connector

mediaSessionConnector.setPlaybackPreparer(object : MediaSessionConnector.PlaybackPreparer {
            override fun onCommand(
                player: Player,
                controlDispatcher: ControlDispatcher,
                command: String,
                extras: Bundle?,
                cb: ResultReceiver?
            ) = false

            override fun getSupportedPrepareActions(): Long {
                return PlaybackStateCompat.ACTION_PREPARE_FROM_MEDIA_ID or
                        PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID or
                        PlaybackStateCompat.ACTION_PREPARE_FROM_SEARCH or
                        PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH
            }

            override fun onPrepare(playWhenReady: Boolean) {

            }

            override fun onPrepareFromMediaId(
                mediaId: String,
                playWhenReady: Boolean,
                extras: Bundle
            ) {

            }

            override fun onPrepareFromSearch(
                query: String,
                playWhenReady: Boolean,
                extras: Bundle
            ) {
  
                if (query.isBlank().not()) {
                    mMainViewModel.search(query)
                }
            }

            override fun onPrepareFromUri(uri: Uri, playWhenReady: Boolean, extras: Bundle) {

            }

        })

but onPrepareFromSearch is never triggered even if app is in foreground. I also have this intent filter in my player activity

   <intent-filter>
            <action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

And the activity is opened ok, when I run this command with adb (but onPrepareFromSearch isn't triggered)

adb shell am start -a android.media.action.MEDIA_PLAY_FROM_SEARCH

Furthermore, I can manually trigger the onPrepareFromSearch callback with this piece of code

   mediaSession.controller.transportControls.playFromSearch("test",null)

but can't trigger it with voice commands in any way.

The media session is set to active. I am testing on a real device. The app isn't published yet on playstore.

I have checkout out the tv-samples apps, and it seems neither the classic kotlin or the the tv reference app use a PlayBackPreparer for the media session connector. I have checked all the documentation I could find, but I cant figure out why the voice command "Play content name" isn't triggering the onPrepareFromSearch when the app is opened (in foreground) and the media session is active.

4

0 回答 0