com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager

Here are the examples of the java api class com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager taken from open source projects.

1. CastApplication#loadQueue()

Project: android-and-chromecast-app
File: CastApplication.java
/**
     * Loading queue items. The only reason we are using this instead of using the VideoCastManager
     * directly is to get around an issue on receiver side for HLS + VTT for a queue; this will be
     * addressed soon and the following workaround will be removed.
     */
public void loadQueue(MediaQueueItem[] items, int startIndex) throws TransientNetworkDisconnectionException, NoConnectionException {
    final VideoCastManager castManager = VideoCastManager.getInstance();
    castManager.addVideoCastConsumer(new VideoCastConsumerImpl() {

        @Override
        public void onMediaQueueOperationResult(int operationId, int statusCode) {
            if (operationId == VideoCastManager.QUEUE_OPERATION_LOAD) {
                if (statusCode == CastStatusCodes.SUCCESS) {
                    castManager.setActiveTrackIds(new long[] {});
                }
                castManager.removeVideoCastConsumer(this);
            }
        }
    });
    castManager.queueLoad(items, startIndex, MediaStatus.REPEAT_MODE_REPEAT_OFF, null);
}

2. Utils#showQueuePopup()

Project: android-and-chromecast-app
File: Utils.java
/**
     * Show a popup to select whether the selected item should play immediately, be added to the
     * end of queue or be added to the queue right after the current item.
     */
public static void showQueuePopup(final Context context, View view, final MediaInfo mediaInfo) {
    final VideoCastManager castManager = VideoCastManager.getInstance();
    final QueueDataProvider provider = QueueDataProvider.getInstance();
    if (!castManager.isConnected()) {
        Log.w(TAG, "showQueuePopup(): not connected to a cast device");
        return;
    }
    PopupMenu popup = new PopupMenu(context, view);
    popup.getMenuInflater().inflate(provider.isQueueDetached() || provider.getCount() == 0 ? R.menu.detached_popup_add_to_queue : R.menu.popup_add_to_queue, popup.getMenu());
    PopupMenu.OnMenuItemClickListener clickListener = new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            QueueDataProvider provider = QueueDataProvider.getInstance();
            MediaQueueItem queueItem = new MediaQueueItem.Builder(mediaInfo).setAutoplay(true).setPreloadTime(CastApplication.PRELOAD_TIME_S).build();
            MediaQueueItem[] newItemArray = new MediaQueueItem[] { queueItem };
            String toastMessage = null;
            try {
                if (provider.isQueueDetached() && provider.getCount() > 0) {
                    switch(menuItem.getItemId()) {
                        case R.id.action_play_now:
                        case R.id.action_add_to_queue:
                            MediaQueueItem[] items = com.google.android.libraries.cast.companionlibrary.utils.Utils.rebuildQueueAndAppend(provider.getItems(), queueItem);
                            // temporary castManager.queueLoad(items, provider.getCount(),
                            // temporary        MediaStatus.REPEAT_MODE_REPEAT_OFF, null);
                            ((CastApplication) context.getApplicationContext()).loadQueue(items, provider.getCount());
                            break;
                        default:
                            return false;
                    }
                } else {
                    if (provider.getCount() == 0) {
                        // temporary castManager.queueLoad(newItemArray, 0,
                        // temporary        MediaStatus.REPEAT_MODE_REPEAT_OFF, null);
                        ((CastApplication) context.getApplicationContext()).loadQueue(newItemArray, 0);
                    } else {
                        int currentId = provider.getCurrentItemId();
                        switch(menuItem.getItemId()) {
                            case R.id.action_play_now:
                                castManager.queueInsertBeforeCurrentAndPlay(queueItem, currentId, null);
                                break;
                            case R.id.action_play_next:
                                int currentPosition = provider.getPositionByItemId(currentId);
                                if (currentPosition == provider.getCount() - 1) {
                                    //we are adding to the end of queue
                                    castManager.queueAppendItem(queueItem, null);
                                } else {
                                    int nextItemId = provider.getItem(currentPosition + 1).getItemId();
                                    castManager.queueInsertItems(newItemArray, nextItemId, null);
                                }
                                toastMessage = context.getString(R.string.queue_item_added_to_play_next);
                                break;
                            case R.id.action_add_to_queue:
                                castManager.queueAppendItem(queueItem, null);
                                toastMessage = context.getString(R.string.queue_item_added_to_queue);
                                break;
                            default:
                                return false;
                        }
                    }
                }
            } catch (NoConnectionExceptionTransientNetworkDisconnectionException |  e) {
                Log.e(TAG, "Failed to add item to queue or play remotely", e);
            }
            if (toastMessage != null) {
                Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    };
    popup.setOnMenuItemClickListener(clickListener);
    popup.show();
}