Search in sources :

Example 1 with PlayerStatus

use of de.danoeh.antennapod.playback.base.PlayerStatus in project AntennaPod by AntennaPod.

the class PlaybackService method handleKeycode.

/**
 * Handles media button events
 * return: keycode was handled
 */
private boolean handleKeycode(int keycode, boolean notificationButton) {
    Log.d(TAG, "Handling keycode: " + keycode);
    final PlaybackServiceMediaPlayer.PSMPInfo info = mediaPlayer.getPSMPInfo();
    final PlayerStatus status = info.playerStatus;
    switch(keycode) {
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            if (status == PlayerStatus.PLAYING) {
                mediaPlayer.pause(!UserPreferences.isPersistNotify(), false);
            } else if (status == PlayerStatus.PAUSED || status == PlayerStatus.PREPARED) {
                mediaPlayer.resume();
            } else if (status == PlayerStatus.PREPARING) {
                mediaPlayer.setStartWhenPrepared(!mediaPlayer.isStartWhenPrepared());
            } else if (status == PlayerStatus.INITIALIZED) {
                mediaPlayer.setStartWhenPrepared(true);
                mediaPlayer.prepare();
            } else if (mediaPlayer.getPlayable() == null) {
                startPlayingFromPreferences();
            } else {
                return false;
            }
            taskManager.restartSleepTimer();
            return true;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
            if (status == PlayerStatus.PAUSED || status == PlayerStatus.PREPARED) {
                mediaPlayer.resume();
            } else if (status == PlayerStatus.INITIALIZED) {
                mediaPlayer.setStartWhenPrepared(true);
                mediaPlayer.prepare();
            } else if (mediaPlayer.getPlayable() == null) {
                startPlayingFromPreferences();
            } else {
                return false;
            }
            taskManager.restartSleepTimer();
            return true;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            if (status == PlayerStatus.PLAYING) {
                mediaPlayer.pause(!UserPreferences.isPersistNotify(), false);
                return true;
            }
            return false;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
            if (!notificationButton) {
                // Handle remapped button as notification button which is not remapped again.
                return handleKeycode(UserPreferences.getHardwareForwardButton(), true);
            } else if (getStatus() == PlayerStatus.PLAYING || getStatus() == PlayerStatus.PAUSED) {
                mediaPlayer.skip();
                return true;
            }
            return false;
        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
            if (getStatus() == PlayerStatus.PLAYING || getStatus() == PlayerStatus.PAUSED) {
                mediaPlayer.seekDelta(UserPreferences.getFastForwardSecs() * 1000);
                return true;
            }
            return false;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            if (!notificationButton) {
                // Handle remapped button as notification button which is not remapped again.
                return handleKeycode(UserPreferences.getHardwarePreviousButton(), true);
            } else if (getStatus() == PlayerStatus.PLAYING || getStatus() == PlayerStatus.PAUSED) {
                mediaPlayer.seekTo(0);
                return true;
            }
            return false;
        case KeyEvent.KEYCODE_MEDIA_REWIND:
            if (getStatus() == PlayerStatus.PLAYING || getStatus() == PlayerStatus.PAUSED) {
                mediaPlayer.seekDelta(-UserPreferences.getRewindSecs() * 1000);
                return true;
            }
            return false;
        case KeyEvent.KEYCODE_MEDIA_STOP:
            if (status == PlayerStatus.PLAYING) {
                mediaPlayer.pause(true, true);
            }
            // gets rid of persistent notification
            stateManager.stopForeground(true);
            return true;
        default:
            Log.d(TAG, "Unhandled key code: " + keycode);
            if (info.playable != null && info.playerStatus == PlayerStatus.PLAYING) {
                // only notify the user about an unknown key event if it is actually doing something
                String message = String.format(getResources().getString(R.string.unknown_media_key), keycode);
                Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
            }
    }
    return false;
}
Also used : PlayerStatus(de.danoeh.antennapod.playback.base.PlayerStatus) PlaybackServiceMediaPlayer(de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer)

Example 2 with PlayerStatus

use of de.danoeh.antennapod.playback.base.PlayerStatus in project AntennaPod by AntennaPod.

the class PlaybackService method setupNotification.

/**
 * Prepares notification and starts the service in the foreground.
 */
private synchronized void setupNotification(final Playable playable) {
    Log.d(TAG, "setupNotification");
    if (playableIconLoaderThread != null) {
        playableIconLoaderThread.interrupt();
    }
    if (playable == null || mediaPlayer == null) {
        Log.d(TAG, "setupNotification: playable=" + playable);
        Log.d(TAG, "setupNotification: mediaPlayer=" + mediaPlayer);
        if (!stateManager.hasReceivedValidStartCommand()) {
            stateManager.stopService();
        }
        return;
    }
    PlayerStatus playerStatus = mediaPlayer.getPlayerStatus();
    notificationBuilder.setPlayable(playable);
    notificationBuilder.setMediaSessionToken(mediaSession.getSessionToken());
    notificationBuilder.setPlayerStatus(playerStatus);
    notificationBuilder.updatePosition(getCurrentPosition(), getCurrentPlaybackSpeed());
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(R.id.notification_playing, notificationBuilder.build());
    startForegroundIfPlaying(playerStatus);
    if (!notificationBuilder.isIconCached()) {
        playableIconLoaderThread = new Thread(() -> {
            Log.d(TAG, "Loading notification icon");
            notificationBuilder.loadIcon();
            if (!Thread.currentThread().isInterrupted()) {
                notificationManager.notify(R.id.notification_playing, notificationBuilder.build());
                updateMediaSessionMetadata(playable);
            }
        });
        playableIconLoaderThread.start();
    }
}
Also used : PlayerStatus(de.danoeh.antennapod.playback.base.PlayerStatus) NotificationManagerCompat(androidx.core.app.NotificationManagerCompat)

Aggregations

PlayerStatus (de.danoeh.antennapod.playback.base.PlayerStatus)2 NotificationManagerCompat (androidx.core.app.NotificationManagerCompat)1 PlaybackServiceMediaPlayer (de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer)1