Search in sources :

Example 1 with CastException

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

the class RemotePSMP method endPlayback.

@Override
protected Future<?> endPlayback(boolean hasEnded, boolean wasSkipped, boolean shouldContinue, boolean toStoppedState) {
    Log.d(TAG, "endPlayback() called");
    boolean isPlaying = playerStatus == PlayerStatus.PLAYING;
    if (playerStatus != PlayerStatus.INDETERMINATE) {
        setPlayerStatus(PlayerStatus.INDETERMINATE, media);
    }
    if (media != null && wasSkipped) {
        // current position only really matters when we skip
        int position = getPosition();
        if (position >= 0) {
            media.setPosition(position);
        }
    }
    final Playable currentMedia = media;
    Playable nextMedia = null;
    if (shouldContinue) {
        nextMedia = callback.getNextInQueue(currentMedia);
        boolean playNextEpisode = isPlaying && nextMedia != null && UserPreferences.isFollowQueue();
        if (playNextEpisode) {
            Log.d(TAG, "Playback of next episode will start immediately.");
        } else if (nextMedia == null) {
            Log.d(TAG, "No more episodes available to play");
        } else {
            Log.d(TAG, "Loading next episode, but not playing automatically.");
        }
        if (nextMedia != null) {
            callback.onPlaybackEnded(nextMedia.getMediaType(), !playNextEpisode);
            // setting media to null signals to playMediaObject() that we're taking care of post-playback processing
            media = null;
            playMediaObject(nextMedia, false, true, /*TODO for now we always stream*/
            playNextEpisode, playNextEpisode);
        }
    }
    if (shouldContinue || toStoppedState) {
        boolean shouldPostProcess = true;
        if (nextMedia == null) {
            try {
                castMgr.stop();
                shouldPostProcess = false;
            } catch (CastException | TransientNetworkDisconnectionException | NoConnectionException e) {
                Log.e(TAG, "Unable to stop playback", e);
                callback.onPlaybackEnded(null, true);
                stop();
            }
        }
        if (shouldPostProcess) {
            // Otherwise we rely on the chromecast callback to tell us the playback has stopped.
            callback.onPostPlayback(currentMedia, hasEnded, wasSkipped, nextMedia != null);
        }
    } else if (isPlaying) {
        callback.onPlaybackPause(currentMedia, currentMedia != null ? currentMedia.getPosition() : INVALID_TIME);
    }
    FutureTask<?> future = new FutureTask<>(() -> {
    }, null);
    future.run();
    return future;
}
Also used : FutureTask(java.util.concurrent.FutureTask) 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) CastException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)

Example 2 with CastException

use of com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException in project zype-android by zype.

the class VideoCastNotificationService method setUpNotification.

private void setUpNotification(final MediaInfo info) throws TransientNetworkDisconnectionException, NoConnectionException {
    if (info == null) {
        return;
    }
    if (mBitmapDecoderTask != null) {
        mBitmapDecoderTask.cancel(false);
    }
    Uri imgUri = null;
    try {
        if (!info.getMetadata().hasImages()) {
            build(info, null, mIsPlaying);
            return;
        } else {
            imgUri = info.getMetadata().getImages().get(0).getUrl();
        }
    } catch (CastException e) {
        LOGE(TAG, "Failed to build notification", e);
    }
    mBitmapDecoderTask = new FetchBitmapTask() {

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            try {
                mVideoArtBitmap = Utils.scaleAndCenterCropBitmap(bitmap, mDimensionInPixels, mDimensionInPixels);
                build(info, mVideoArtBitmap, mIsPlaying);
            } catch (CastException | NoConnectionException | TransientNetworkDisconnectionException e) {
                LOGE(TAG, "Failed to set notification for " + info.toString(), e);
            }
            if (mVisible && (mNotification != null)) {
                startForeground(NOTIFICATION_ID, mNotification);
            }
            if (this == mBitmapDecoderTask) {
                mBitmapDecoderTask = null;
            }
        }
    };
    mBitmapDecoderTask.execute(imgUri);
}
Also used : Bitmap(android.graphics.Bitmap) FetchBitmapTask(com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask) Uri(android.net.Uri) CastException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)

Example 3 with CastException

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

the class VideoCastNotificationService method setUpNotification.

private void setUpNotification(final MediaInfo info) throws TransientNetworkDisconnectionException, NoConnectionException {
    if (info == null) {
        return;
    }
    if (mBitmapDecoderTask != null) {
        mBitmapDecoderTask.cancel(false);
    }
    Uri imgUri = null;
    try {
        if (!info.getMetadata().hasImages()) {
            build(info, null, mIsPlaying);
            return;
        } else {
            imgUri = info.getMetadata().getImages().get(0).getUrl();
        }
    } catch (CastException e) {
        LOGE(TAG, "Failed to build notification", e);
    }
    mBitmapDecoderTask = new FetchBitmapTask() {

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            try {
                mVideoArtBitmap = Utils.scaleAndCenterCropBitmap(bitmap, mDimensionInPixels, mDimensionInPixels);
                build(info, mVideoArtBitmap, mIsPlaying);
            } catch (CastException | NoConnectionException | TransientNetworkDisconnectionException e) {
                LOGE(TAG, "Failed to set notification for " + info.toString(), e);
            }
            if (mVisible && (mNotification != null)) {
                startForeground(NOTIFICATION_ID, mNotification);
            }
            if (this == mBitmapDecoderTask) {
                mBitmapDecoderTask = null;
            }
        }
    };
    mBitmapDecoderTask.execute(imgUri);
}
Also used : Bitmap(android.graphics.Bitmap) FetchBitmapTask(com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask) Uri(android.net.Uri) CastException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)

Example 4 with CastException

use of com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException 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 5 with CastException

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

Aggregations

CastException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)5 NoConnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException)3 TransientNetworkDisconnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException)3 Bitmap (android.graphics.Bitmap)2 Uri (android.net.Uri)2 FetchBitmapTask (com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask)2 SuppressLint (android.annotation.SuppressLint)1 Intent (android.content.Intent)1 SQLiteException (android.database.sqlite.SQLiteException)1 AudioAttributes (android.media.AudioAttributes)1 AudioFocusRequest (android.media.AudioFocusRequest)1 KeyEvent (android.view.KeyEvent)1 VideoCastManager (com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager)1 Playable (de.danoeh.antennapod.core.util.playback.Playable)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 FutureTask (java.util.concurrent.FutureTask)1