private void handleKeyEvent(KeyEvent event) {
    if (event.getAction() != KeyEvent.ACTION_DOWN || event.getRepeatCount() > 0) {
      return;
    }

    switch (event.getKeyCode()) {
      case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
      case KeyEvent.KEYCODE_HEADSETHOOK:
        downloadService.togglePlayPause();
        break;
      case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        downloadService.previous();
        break;
      case KeyEvent.KEYCODE_MEDIA_NEXT:
        if (downloadService.getCurrentPlayingIndex() < downloadService.size() - 1) {
          downloadService.next();
        }
        break;
      case KeyEvent.KEYCODE_MEDIA_STOP:
        downloadService.reset();
        break;
      case KeyEvent.KEYCODE_MEDIA_PLAY:
        downloadService.play();
        break;
      case KeyEvent.KEYCODE_MEDIA_PAUSE:
        downloadService.pause();
        break;
      default:
        break;
    }
  }
Esempio n. 2
0
 /** Plays or resumes the playback, depending on the current player state. */
 public synchronized void togglePlayPause() {
   if (playerState == PAUSED || playerState == COMPLETED) {
     start();
   } else if (playerState == STOPPED || playerState == IDLE) {
     play();
   } else if (playerState == STARTED) {
     pause();
   }
 }
 @Override
 public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   Log.i(TAG, "intentReceiver.onReceive: " + action);
   if (DownloadServiceImpl.CMD_PLAY.equals(action)) {
     downloadService.play();
   } else if (DownloadServiceImpl.CMD_NEXT.equals(action)) {
     downloadService.next();
   } else if (DownloadServiceImpl.CMD_PREVIOUS.equals(action)) {
     downloadService.previous();
   } else if (DownloadServiceImpl.CMD_TOGGLEPAUSE.equals(action)) {
     downloadService.togglePlayPause();
   } else if (DownloadServiceImpl.CMD_PAUSE.equals(action)) {
     downloadService.pause();
   } else if (DownloadServiceImpl.CMD_STOP.equals(action)) {
     downloadService.pause();
     downloadService.seekTo(0);
   }
 }