Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture UIMediaController clicks via casting control interface

I've built a custom cast sender in my app that works perfectly - the only feature I need to add is a way to listen for the 'Play/Pause' and 'Skip' buttons, but Google's documentation (as far as I've been able to search for) doesn't give any clues, and after adding the functionality shown in Advanced Cast Features Docs did not work:

CastMediaOptions castMediaOptions = new CastMediaOptions.Builder()
     .setMediaIntentReceiverClassName(CastIntentReceiver.class.getName())
     .build();

return new CastOptions.Builder()
        .setReceiverApplicationId(context.getString(R.string.cast_id))
        .setCastMediaOptions(castMediaOptions)
        .build();

I have a CastIntentReceiver that extends MediaIntentReceiver that's referenced in CastOptionsProvider and in the manifest.

public class CastIntentReceiver extends MediaIntentReceiver {

    public static String TAG = "CastIntentReceiver";

    public CastIntentReceiver() {
        Log.i(TAG, "CastIntentReceiver: Constructed!");
    }
    ...
    // The rest of the Override methods don't do anything

}

The receiver is working, because the Log print is working, but none of the Override methods are called (no Log printing).

Here are some additional details on my implementation:

All casting functions are in two separate classes, the CastManager (controller) and the CastControls fragment (view) which is added to the base of the MainActivity View (similar to YouTube). There are also Interfaces that trigger things between these two classes.

In the CastManager, I setup the UIMediaController and bind Views:

View view = CastingBridge.CAST_CONTROLS_VIEW.findViewById(R.id.cast_drawer_controls);

uiMediaController = new UIMediaController(activity);
SeekBar seekBar = (SeekBar) view.findViewById(R.id.cast_control_seek_bar);
TextView start = (TextView) view.findViewById(R.id.cast_control_time_start);
TextView end = (TextView) view.findViewById(R.id.cast_control_time_end);
ImageButton playPauseButton = (ImageButton) view.findViewById(R.id.cast_control_play_pause);
ImageButton rewindButton = (ImageButton) view.findViewById(R.id.cast_control_rewind);
ImageButton forwardButton = (ImageButton) view.findViewById(R.id.cast_control_forward);
ImageButton stopButton = (ImageButton) view.findViewById(R.id.cast_control_stop);
ProgressBar loadingSpinner = (ProgressBar) view.findViewById(R.id.cast_control_loading);

As stated before, all these work as they should, but I need to listen for (capture) the touch/click events so that when a video is paused or stopped, I can save the current watch length for resume purposes.

How do I implement MediaIntentReceiver to listen for these events? I've tried adding click listeners to the individual Views, but as expected, @Override onClick removes the initial intended functionality.

The documentation specifies what seem to be the methods I need, but how is this referenced?

I also tried adding the only listener available to UIMediaController:

uiMediaController.setPostRemoteMediaClientListener(new RemoteMediaClient.Listener() {
    @Override
    public void onStatusUpdated() {
        Log.i(TAG, "onStatusUpdated: Status Updated");
    }
});

This, however does not do anything, as the onStatusUpdated method supplies no arguments.

Can anyone help me shed some light onto this? I've been trying different things and searching for a couple days now, so time to ask for help.

Thanks!

like image 966
mwieczorek Avatar asked Jul 31 '16 10:07

mwieczorek


1 Answers

The receiver you have defined (and MediaIntentReceiver in general) is used to handle actions from notification, lockscreen and cast dialog; so having a custom one affects the behavior when it is initiated from those places. If you are using UIMediaController, then you'd need to use onSendingRemoteMediaRequest() to be notified when user interacts with these controls.

like image 119
Ali Naddaf Avatar answered Oct 18 '22 21:10

Ali Naddaf