Search in sources :

Example 11 with TransientNetworkDisconnectionException

use of com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException in project Shuttle by timusus.

the class VideoCastManager method onVolumeChanged.

private void onVolumeChanged() {
    LOGD(TAG, "onVolumeChanged() reached");
    double volume;
    try {
        volume = getVolume();
        boolean isMute = isMute();
        for (VideoCastConsumer consumer : mVideoConsumers) {
            consumer.onVolumeChanged(volume, isMute);
        }
    } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
        LOGE(TAG, "Failed to get volume", e);
    }
}
Also used : VideoCastConsumer(com.google.android.libraries.cast.companionlibrary.cast.callbacks.VideoCastConsumer) NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException)

Example 12 with TransientNetworkDisconnectionException

use of com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException in project Shuttle by timusus.

the class VideoCastManager method onRemoteMediaPlayerStatusUpdated.

/*
     * This is called by onStatusUpdated() of the RemoteMediaPlayer
     */
private void onRemoteMediaPlayerStatusUpdated() {
    LOGD(TAG, "onRemoteMediaPlayerStatusUpdated() reached");
    if (mApiClient == null || mRemoteMediaPlayer == null || mRemoteMediaPlayer.getMediaStatus() == null) {
        LOGD(TAG, "mApiClient or mRemoteMediaPlayer is null, so will not proceed");
        return;
    }
    mMediaStatus = mRemoteMediaPlayer.getMediaStatus();
    List<MediaQueueItem> queueItems = mMediaStatus.getQueueItems();
    if (queueItems != null) {
        int itemId = mMediaStatus.getCurrentItemId();
        MediaQueueItem item = mMediaStatus.getQueueItemById(itemId);
        int repeatMode = mMediaStatus.getQueueRepeatMode();
        onQueueUpdated(queueItems, item, repeatMode, false);
    } else {
        onQueueUpdated(null, null, MediaStatus.REPEAT_MODE_REPEAT_OFF, false);
    }
    mState = mMediaStatus.getPlayerState();
    mIdleReason = mMediaStatus.getIdleReason();
    try {
        double volume = getVolume();
        boolean isMute = isMute();
        boolean makeUiHidden = false;
        if (mState == MediaStatus.PLAYER_STATE_PLAYING) {
            LOGD(TAG, "onRemoteMediaPlayerStatusUpdated(): Player status = playing");
            updateMediaSession(true);
            long mediaDurationLeft = getMediaTimeRemaining();
            startReconnectionService(mediaDurationLeft);
            startNotificationService();
        } else if (mState == MediaStatus.PLAYER_STATE_PAUSED) {
            LOGD(TAG, "onRemoteMediaPlayerStatusUpdated(): Player status = paused");
            updateMediaSession(false);
            startNotificationService();
        } else if (mState == MediaStatus.PLAYER_STATE_IDLE) {
            LOGD(TAG, "onRemoteMediaPlayerStatusUpdated(): Player status = IDLE with reason: " + mIdleReason);
            updateMediaSession(false);
            switch(mIdleReason) {
                case MediaStatus.IDLE_REASON_FINISHED:
                    if (mMediaStatus.getLoadingItemId() == MediaQueueItem.INVALID_ITEM_ID) {
                        // we have reached the end of queue
                        clearMediaSession();
                        makeUiHidden = true;
                    }
                    break;
                case MediaStatus.IDLE_REASON_ERROR:
                    // something bad happened on the cast device
                    LOGD(TAG, "onRemoteMediaPlayerStatusUpdated(): IDLE reason = ERROR");
                    makeUiHidden = true;
                    clearMediaSession();
                    onFailed(R.string.ccl_failed_receiver_player_error, NO_STATUS_CODE);
                    break;
                case MediaStatus.IDLE_REASON_CANCELED:
                    LOGD(TAG, "onRemoteMediaPlayerStatusUpdated(): IDLE reason = CANCELLED");
                    makeUiHidden = !isRemoteStreamLive();
                    break;
                case MediaStatus.IDLE_REASON_INTERRUPTED:
                    if (mMediaStatus.getLoadingItemId() == MediaQueueItem.INVALID_ITEM_ID) {
                        // we have reached the end of queue
                        clearMediaSession();
                        makeUiHidden = true;
                    }
                    break;
                default:
                    LOGE(TAG, "onRemoteMediaPlayerStatusUpdated(): Unexpected Idle Reason " + mIdleReason);
            }
        } else if (mState == MediaStatus.PLAYER_STATE_BUFFERING) {
            LOGD(TAG, "onRemoteMediaPlayerStatusUpdated(): Player status = buffering");
        } else {
            LOGD(TAG, "onRemoteMediaPlayerStatusUpdated(): Player status = unknown");
            makeUiHidden = true;
        }
        if (makeUiHidden) {
            stopReconnectionService();
            stopNotificationService();
        }
        updateMiniControllersVisibility(!makeUiHidden);
        updateMiniControllers();
        for (VideoCastConsumer consumer : mVideoConsumers) {
            consumer.onRemoteMediaPlayerStatusUpdated();
            consumer.onVolumeChanged(volume, isMute);
        }
    } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
        LOGE(TAG, "Failed to get volume state due to network issues", e);
    }
}
Also used : VideoCastConsumer(com.google.android.libraries.cast.companionlibrary.cast.callbacks.VideoCastConsumer) NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException) MediaQueueItem(com.google.android.gms.cast.MediaQueueItem) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point)

Example 13 with TransientNetworkDisconnectionException

use of com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException in project Shuttle by timusus.

the class VideoIntentReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action == null) {
        return;
    }
    VideoCastManager castManager = VideoCastManager.getInstance();
    switch(action) {
        case VideoCastNotificationService.ACTION_TOGGLE_PLAYBACK:
            try {
                castManager.togglePlayback();
            } catch (CastException | TransientNetworkDisconnectionException | NoConnectionException e) {
                LOGE(TAG, "onReceive() Failed to toggle playback");
            }
            break;
        case VideoCastNotificationService.ACTION_PLAY_NEXT:
            try {
                castManager.queueNext(null);
            } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
                LOGE(TAG, "onReceive() Failed to skip to the next in queue");
            }
            break;
        case VideoCastNotificationService.ACTION_PLAY_PREV:
            try {
                castManager.queuePrev(null);
            } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
                LOGE(TAG, "onReceive() Failed to skip to the previous in queue");
            }
            break;
        case VideoCastNotificationService.ACTION_FORWARD:
            int forwardAmount = intent.getIntExtra(VideoCastNotificationService.EXTRA_FORWARD_STEP_MS, 0);
            try {
                castManager.forward(forwardAmount);
            } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
                LOGE(TAG, "onReceive() Failed to forward the media");
            }
            break;
        case VideoCastNotificationService.ACTION_REWIND:
            int rewindAmount = intent.getIntExtra(VideoCastNotificationService.EXTRA_FORWARD_STEP_MS, 0);
            try {
                castManager.forward(rewindAmount);
            } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
                LOGE(TAG, "onReceive() Failed to rewind the media");
            }
            break;
        case VideoCastNotificationService.ACTION_STOP:
            LOGD(TAG, "Calling stopApplication from intent");
            castManager.disconnectDevice(true, true, true);
            // persistent notification if other things go wrong
            if (castManager.getNotificationServiceClass() != null) {
                context.stopService(new Intent(context, castManager.getNotificationServiceClass()));
            }
            break;
        case Intent.ACTION_MEDIA_BUTTON:
            // Lollipop
            if (!intent.hasExtra(Intent.EXTRA_KEY_EVENT)) {
                return;
            }
            KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
            if (keyEvent == null || keyEvent.getAction() != KeyEvent.ACTION_DOWN) {
                return;
            }
            if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
                try {
                    castManager.togglePlayback();
                } catch (CastException | TransientNetworkDisconnectionException | NoConnectionException e) {
                    LOGE(TAG, "onReceive() Failed to toggle playback ");
                }
            }
            break;
    }
}
Also used : KeyEvent(android.view.KeyEvent) VideoCastManager(com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager) NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) Intent(android.content.Intent) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException) CastException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)

Example 14 with TransientNetworkDisconnectionException

use of com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException in project Shuttle by timusus.

the class BaseCastManager method disconnectDevice.

/**
     * Disconnects from the connected device.
     *
     * @param stopAppOnExit If {@code true}, the application running on the cast device will be
     * stopped when disconnected.
     * @param clearPersistedConnectionData If {@code true}, the persisted connection information
     * will be cleared as part of this call.
     * @param setDefaultRoute If {@code true}, after disconnection, the selected route will be set
     * to the Default Route.
     */
public final void disconnectDevice(boolean stopAppOnExit, boolean clearPersistedConnectionData, boolean setDefaultRoute) {
    LOGD(TAG, "disconnectDevice(" + clearPersistedConnectionData + "," + setDefaultRoute + ")");
    if (mSelectedCastDevice == null) {
        return;
    }
    mSelectedCastDevice = null;
    mDeviceName = null;
    String message = "disconnectDevice() Disconnect Reason: ";
    int reason;
    if (mConnectionSuspended) {
        message += "Connectivity lost";
        reason = DISCONNECT_REASON_CONNECTIVITY;
    } else {
        switch(mApplicationErrorCode) {
            case CastStatusCodes.APPLICATION_NOT_RUNNING:
                message += "App was taken over or not available anymore";
                reason = DISCONNECT_REASON_APP_NOT_RUNNING;
                break;
            case NO_APPLICATION_ERROR:
                message += "Intentional disconnect";
                reason = DISCONNECT_REASON_EXPLICIT;
                break;
            default:
                message += "Other";
                reason = DISCONNECT_REASON_OTHER;
        }
    }
    LOGD(TAG, message);
    for (BaseCastConsumer consumer : mBaseCastConsumers) {
        consumer.onDisconnectionReason(reason);
    }
    LOGD(TAG, "mConnectionSuspended: " + mConnectionSuspended);
    if (!mConnectionSuspended && clearPersistedConnectionData) {
        clearPersistedConnectionInfo(CLEAR_ALL);
        stopReconnectionService();
    }
    try {
        if ((isConnected() || isConnecting()) && stopAppOnExit) {
            LOGD(TAG, "Calling stopApplication");
            stopApplication();
        }
    } catch (NoConnectionException | TransientNetworkDisconnectionException e) {
        LOGE(TAG, "Failed to stop the application after disconnecting route", e);
    }
    onDeviceUnselected();
    if (mApiClient != null) {
        // will throw an exception
        if (mApiClient.isConnected()) {
            LOGD(TAG, "Trying to disconnect");
            mApiClient.disconnect();
        }
        if ((mMediaRouter != null) && setDefaultRoute) {
            LOGD(TAG, "disconnectDevice(): Setting route to default");
            mMediaRouter.selectRoute(mMediaRouter.getDefaultRoute());
        }
        mApiClient = null;
    }
    mSessionId = null;
    onDisconnected(stopAppOnExit, clearPersistedConnectionData, setDefaultRoute);
}
Also used : NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException) BaseCastConsumer(com.google.android.libraries.cast.companionlibrary.cast.callbacks.BaseCastConsumer)

Example 15 with TransientNetworkDisconnectionException

use of com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException in project AntennaPod by AntennaPod.

the class RemotePSMP method playMediaObject.

/**
     * Internal implementation of playMediaObject. This method has an additional parameter that allows the caller to force a media player reset even if
     * the given playable parameter is the same object as the currently playing media.
     *
     * @see #playMediaObject(de.danoeh.antennapod.core.util.playback.Playable, boolean, boolean, boolean)
     */
private void playMediaObject(@NonNull final Playable playable, final boolean forceReset, final boolean stream, final boolean startWhenPrepared, final boolean prepareImmediately) {
    if (!CastUtils.isCastable(playable)) {
        Log.d(TAG, "media provided is not compatible with cast device");
        callback.onMediaPlayerInfo(CAST_ERROR_PRIORITY_HIGH, R.string.cast_not_castable);
        Playable nextPlayable = playable;
        do {
            nextPlayable = callback.getNextInQueue(nextPlayable);
        } while (nextPlayable != null && !CastUtils.isCastable(nextPlayable));
        if (nextPlayable != null) {
            playMediaObject(nextPlayable, forceReset, stream, startWhenPrepared, prepareImmediately);
        }
        return;
    }
    if (media != null) {
        if (!forceReset && media.getIdentifier().equals(playable.getIdentifier()) && playerStatus == PlayerStatus.PLAYING) {
            // episode is already playing -> ignore method call
            Log.d(TAG, "Method call to playMediaObject was ignored: media file already playing.");
            return;
        } else {
            // set temporarily to pause in order to update list with current position
            boolean isPlaying = playerStatus == PlayerStatus.PLAYING;
            int position = media.getPosition();
            try {
                isPlaying = castMgr.isRemoteMediaPlaying();
                position = (int) castMgr.getCurrentMediaPosition();
            } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
                Log.e(TAG, "Unable to determine whether media was playing, falling back to stored player status", e);
            }
            if (isPlaying) {
                callback.onPlaybackPause(media, position);
            }
            if (!media.getIdentifier().equals(playable.getIdentifier())) {
                final Playable oldMedia = media;
                callback.onPostPlayback(oldMedia, false, false, true);
            }
            setPlayerStatus(PlayerStatus.INDETERMINATE, null);
        }
    }
    this.media = playable;
    remoteMedia = remoteVersion(playable);
    this.mediaType = media.getMediaType();
    this.startWhenPrepared.set(startWhenPrepared);
    setPlayerStatus(PlayerStatus.INITIALIZING, media);
    try {
        media.loadMetadata();
        callback.onMediaChanged(true);
        setPlayerStatus(PlayerStatus.INITIALIZED, media);
        if (prepareImmediately) {
            prepare();
        }
    } catch (Playable.PlayableException e) {
        Log.e(TAG, "Error while loading media metadata", e);
        setPlayerStatus(PlayerStatus.STOPPED, null);
    }
}
Also used : Playable(de.danoeh.antennapod.core.util.playback.Playable) NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException)

Aggregations

NoConnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException)15 TransientNetworkDisconnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException)15 VideoCastConsumer (com.google.android.libraries.cast.companionlibrary.cast.callbacks.VideoCastConsumer)4 SuppressLint (android.annotation.SuppressLint)3 Point (android.graphics.Point)3 CastException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)3 PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 Builder (com.google.android.gms.cast.Cast.CastOptions.Builder)2 MediaInfo (com.google.android.gms.cast.MediaInfo)2 MediaMetadata (com.google.android.gms.cast.MediaMetadata)2 Playable (de.danoeh.antennapod.core.util.playback.Playable)2 TargetApi (android.annotation.TargetApi)1 NotFoundException (android.content.res.Resources.NotFoundException)1 SQLiteException (android.database.sqlite.SQLiteException)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)1 RouteInfo (android.support.v7.media.MediaRouter.RouteInfo)1