Search in sources :

Example 26 with NoConnectionException

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

the class MusicService method play.

/**
 * Starts playback of a previously opened file.
 */
public void play() {
    int status;
    if (ShuttleUtils.hasOreo()) {
        AudioFocusRequest audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).setOnAudioFocusChangeListener(audioFocusListener).setAudioAttributes(new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build()).build();
        this.audioFocusRequest = audioFocusRequest;
        status = audioManager.requestAudioFocus(audioFocusRequest);
    } else {
        status = audioManager.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    }
    if (status != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        return;
    }
    if (playbackLocation == LOCAL) {
        if (SettingsManager.getInstance().getEqualizerEnabled()) {
            // Shutdown any existing external audio sessions
            equalizer.closeEqualizerSessions(false, getAudioSessionId());
            // Start internal equalizer session (will only turn on if enabled)
            equalizer.openEqualizerSession(true, getAudioSessionId());
        } else {
            equalizer.openEqualizerSession(false, getAudioSessionId());
        }
    }
    if (mediaSession != null && !mediaSession.isActive()) {
        try {
            mediaSession.setActive(true);
        } catch (Exception e) {
            Log.e(TAG, "mSession.setActive() failed");
        }
    }
    switch(playbackLocation) {
        case LOCAL:
            {
                if (player != null && player.isInitialized()) {
                    // if we are at the end of the song, go to the next song first
                    final long duration = player.getDuration();
                    if (repeatMode != RepeatMode.ONE && duration > 2000 && player.getPosition() >= duration - 2000) {
                        gotoNext(true);
                    }
                    player.start();
                    // make sure we fade in, in case a previous fadein was stopped
                    // because of another focus loss
                    playerHandler.removeMessages(PlayerHandler.FADE_DOWN);
                    playerHandler.sendEmptyMessage(PlayerHandler.FADE_UP);
                    setIsSupposedToBePlaying(true, true);
                    cancelShutdown();
                    updateNotification();
                } else if (getCurrentPlaylist().size() == 0) {
                    // something.
                    if (queueReloading) {
                        playOnQueueLoad = true;
                    } else {
                        playAutoShuffleList();
                    }
                }
                break;
            }
        case REMOTE:
            {
                // if we are at the end of the song, go to the next song first
                final long duration = player.getDuration();
                if (repeatMode != RepeatMode.ONE && duration > 2000 && player.getPosition() >= duration - 2000) {
                    gotoNext(true);
                }
                if (!isSupposedToBePlaying) {
                    isSupposedToBePlaying = true;
                    notifyChange(InternalIntents.PLAY_STATE_CHANGED);
                }
                cancelShutdown();
                updateNotification();
                switch(playbackState) {
                    case STOPPED:
                        {
                            try {
                                castManager.checkConnectivity();
                                prepareChromeCastLoad(0, true);
                                playbackState = PLAYING;
                                updateNotification();
                            } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
                                Log.e(TAG, "Play() called & failed. State: Stopped " + e.toString());
                                playbackState = STOPPED;
                                updateNotification();
                            }
                            break;
                        }
                    case PAUSED:
                        {
                            try {
                                castManager.checkConnectivity();
                                castManager.play();
                                playbackState = PLAYING;
                                updateNotification();
                            } catch (TransientNetworkDisconnectionException | NoConnectionException | CastException e) {
                                Log.e(TAG, "Play() called & failed. State: Paused " + e.toString());
                                playbackState = PAUSED;
                                updateNotification();
                            }
                            break;
                        }
                }
                if (getCurrentPlaylist().size() == 0) {
                    if (queueReloading) {
                        playOnQueueLoad = true;
                    } else {
                        playAutoShuffleList();
                    }
                }
            }
    }
}
Also used : NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) AudioAttributes(android.media.AudioAttributes) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException) AudioFocusRequest(android.media.AudioFocusRequest) SuppressLint(android.annotation.SuppressLint) SQLiteException(android.database.sqlite.SQLiteException) NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) ConcurrentModificationException(java.util.ConcurrentModificationException) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException) CastException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException) CastException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)

Example 27 with NoConnectionException

use of com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException 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)

Aggregations

NoConnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException)27 TransientNetworkDisconnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException)23 VideoCastConsumer (com.google.android.libraries.cast.companionlibrary.cast.callbacks.VideoCastConsumer)8 MediaInfo (com.google.android.gms.cast.MediaInfo)7 SuppressLint (android.annotation.SuppressLint)5 MediaMetadata (com.google.android.gms.cast.MediaMetadata)4 Point (android.graphics.Point)3 Builder (com.google.android.gms.cast.Cast.CastOptions.Builder)3 CastException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)3 TargetApi (android.annotation.TargetApi)2 PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 NotFoundException (android.content.res.Resources.NotFoundException)2 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)2 RouteInfo (android.support.v7.media.MediaRouter.RouteInfo)2 View (android.view.View)2 OnClickListener (android.view.View.OnClickListener)2 ImageView (android.widget.ImageView)2 SeekBar (android.widget.SeekBar)2 OnSeekBarChangeListener (android.widget.SeekBar.OnSeekBarChangeListener)2