Skip to content

Interact with Video

Hsueh-Jen edited this page May 28, 2020 · 33 revisions

After connect to StraasMediaService(MediaBrowserServiceCompat) via StraasMediaCore, we could use getSupportMediaController() anywhere within this FragmentActivity (including Fragment, View, etc.).

The power of MediaControllerCompat

With MediaControllerCompat, we could do three things:

  • Control playbacks.
  • Get playback information by polling vales or register callback to keep UI synced with Service.
  • Execute our commands or custom actions.

Control playbacks

Currently we support two starting points to play medias. Use playFromMediaId() to play all your contents served by StraaS and playFromUri() to play any video link outside StraaS (e.g. HLS samples).

getSupportMediaController().getTransportControls()
     .playFromMediaId("5aCtv82p", null);

getSupportMediaController().getTransportControls()
     .playFromMediaId(mMediaItem.getMediaId(), null);

getSupportMediaController().getTransportControls()
     .playFromUri(Uri.parse(
          "https://devimages.apple.com.edgekey.net/streaming/examples/" + 
          "bipbop_4x3/bipbop_4x3_variant.m3u8"), null);

// play live event streaming
getSupportMediaController().getTransportControls()
     .playFromMediaId(StraasMediaCore.LIVE_ID_PREFIX + "ypV9mtmz", null);

The second parameter is null currently. If you want to do some extra behavior, e.g. start from 30 secs timestamp, just fill with Bundle in second parameter.

Bundle bundle = new Bundle();
bundle.putLong(StraasMediaCore.PLAY_OPTION_SEEK_TIME, positionMs);
getSupportMediaController().getTransportControls()
    .playFromMediaId(mediaId, bundle);

We wrap a MediaControllerCompatHelper in our sample code to help you execute those actions quickly.

Other common actions just work like a charm such as play(), pause(), stop(), seekTo(), adjustVolume(), etc.

Get playback information

You could get latest data by polling vales or register callback to keep UI synced with Service.

NOTE: If you enable advertisement integration with our SDK:

mStraasClient.setImaHelper(ImaHelper.newInstance());

When rolling with Ad, the getActiveQueueItemId() will be StraasMediaCore.AD_PLAYBACK_ID.

Execute our commands or custom actions.

If you want to get video quality list (e.g. 360p, 720p) or switch current quality to another, we could use sendCommand() and sendCustomAction() to achieve these goals.

We wrap a MediaControllerCompatHelper in our sample code to help you execute those actions quickly. Call MediaControllerCompatHelper.showVideoQualityListDialog(mFragmentActivity) will show a single choice dialog with video quality list, user could switch quality from dialog directly.

Foreground Service

If you want your MediaBrowserServiceCompat run in foreground, you can use custom command COMMAND_FOREGROUND to achieve this.
Here is an example:

MediaControllerCompat controller = MediaControllerCompat.getMediaController(this);
Bundle bundle = new Bundle();
bundle.putParcelable(StraasMediaCore.KEY_NOTIFICATION_OPTIONS, new NotificationOptions.Builder().build());
controller.getTransportControls().sendCustomAction(StraasMediaCore.COMMAND_FOREGROUND, bundle);

You could use NotificationOptions to adjust your Notification.
After sending COMMAND_FOREGROUND, your MediaBrowserServiceCompat will run in foreground and shows a notification when playing.
If you want to disable this feature, you could use COMMAND_STOP_FOREGROUND like the following code:

MediaControllerCompat controller = MediaControllerCompat.getMediaController(this);
controller.getTransportControls().sendCustomAction(StraasMediaCore.COMMAND_STOP_FOREGROUND, null);

Live DVR

DVR feature allows you to pause, rewind, and play from any point in the live streaming event. Once you resume playing, the event will start from where you hit pause, so you won't miss a thing,

  • How does it work?
    • Seeking: Miss something? Seek back and forth in the event by dragging the scrubber backwards and forwards. You'll see the timestamp change as you move the scrubber around, so you'll know where you are.
    • Live: Ready to get back to the real-time coverage of the event? Just click the Live button, and we'll take you there.

First, if you want your player to support DVR feature on live streaming, please check LIVE_DVR_ENABLED first and enable your seek bar and live icon UI only if this field is true, here is an example from StraasPlayerView:

boolean isLiveSeekable = metadata.getBundle().getBoolean(VideoCustomMetadata.LIVE_DVR_ENABLED);
switchMode(true, isLiveSeekable);
...

private void switchMode(boolean isLive, boolean isLiveSeekable) {
        ...
        if (isLive) {
            setContentSeekBarVisibility(mIsLiveSeekable ? VISIBLE : GONE);
            setSummaryViewerVisibility(INVISIBLE);
            setBottomLeftColumnToLiveIcon(true);
        } else {
            ...
        }
    }

Next, for seeking event, player will seek back and forth for the live event and also user can click the live icon button to trigger live event by sending the custom command COMMAND_PLAY_AT_LIVE_EDGE to seek the current playback position back to the real-time coverage of live (live edge) and play automatically. Example from MediaControllerCompatHelper and StraasPlayerView:

private ContentSeekBar.TrackingListener mTrackingListener = new ContentSeekBar.TrackingListener() {
        @Override
        public void onTrackingTouch(boolean isTracking) {
            if (isTracking) {
                ...
                if (mIsLiveSeekable) {
                    setBottomLeftColumnToLivePositionTime();
                }
            } else {
                refreshLiveDvrUiStatus(PLAYBACK_MODE_LIVE_DVR);
            }
        }
    };


public static void playAtLiveEdge(@NonNull MediaControllerCompat controller) {
    controller.getTransportControls().sendCustomAction(StraasMediaCore.COMMAND_PLAY_AT_LIVE_EDGE, null);
}

private OnClickListener mGrayLiveIconOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            ...
            MediaControllerCompatHelper.playAtLiveEdge(getMediaControllerCompat());
            ...
            refreshLiveDvrUiStatus(PLAYBACK_MODE_LIVE_EDGE);
        }
    };
  • Important things to note:
    • DVR playback will only be available for a limited amount of time after the live event ends. Currently, the recording will remain available for 10 minutes.