ExoPlayer: How can I get the callback when Google Assistant command is triggered?
Searched documentation and issues
Official ExoPlayer documentation and source code of MediaControllerCompat, MediaSessionConnector, MediaSession classes.
My issue is similar to #6057 and #6446 but none of the solutions worked as expected (details on question).
Question
I have an Android cordova app which has Exoplayer Media Session enabled, the player was not resuming after seeking on AndroidTV using Google Assistant.
So I tried to save the playWhenReady on the dispatchSetPlayWhenReady(which is called when Google Assistant is opened) in a local property wasPlaying, to check if it should resume after seeking (if it was playing before).
private class MyControlDispatcher implements ControlDispatcher {
private boolean wasPlaying = false;
@Override
public boolean dispatchSetPlayWhenReady(Player player, boolean playWhenReady) {
this.wasPlaying = player.getPlayWhenReady();
player.setPlayWhenReady(playWhenReady);
return true;
}
@Override
public boolean dispatchSeekTo(Player player, int windowIndex, long positionMs) {
if (this.wasPlaying) {
player.setPlayWhenReady(true);
}
player.seekTo(windowIndex, positionMs);
return true;
}
...
}
MyControlDispatcher myControlDispatcher = new MyControlDispatcher();
MediaSessionConnector mediaSessionConnector = new MediaSessionConnector(mediaSession);
mediaSessionConnector.setControlDispatcher(myControlDispatcher);
That solved the initial problem, but then I have a problem when user pauses and then seeks, both using Google Assistant. It will save wasPlaying as true when user calls PAUSE command (on the dispatchSetPlayWhenReady when assistant is opened) and resume after seeking when it shouldn’t.
So basically I need to somehow get the PAUSE command callback from google assistant so I can differentiate from the dispatchSetPlayWhenReady which is called when assistant is opened. And guarantee that wasPlaying is set to false when user calls PAUSE command and prevent it from resuming after seeking.
Tried so far:
mediaSession.setCallback(new MediaSessionCompat.Callback() {
public void onPause() {
wasPlaying = false;
super.onPause();
}
...
});
And:
player.addListener(new Player.EventListener() {
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == PlaybackStateCompat.STATE_PAUSED) {
wasPlaying = false;
}
}
});
But none of them are called on PAUSE command of Google Assistant, but only when it’s opened.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 22 (12 by maintainers)
This should be in 2.11.5, which will hopefully go out sometime next week.
That looks interesting. Thanks for your comment and the PR. I look into this.
I think the problem above is that Assistant is sending a pause event which is not expected, but it’s very well possible this is caused by the same reason.
@aureobeck Seems like the semantics described by @inv3rse indicates the problem is in the MediaSessionConnector.