Esempio n. 1
0
  private void refreshPodcast() {
    if (_player == null) return;

    PlayerStatus status = PlayerStatus.getCurrentState(this);
    if (!status.hasActivePodcast()) {
      _player.stop();
      return;
    }

    if (status.getPodcastId() != _currentPodcastId) {
      _currentPodcastId = status.getPodcastId();
      if (!_player.prepare(status.getFilename(), status.getPosition())) {
        _player.stop();
        String toastMessage =
            getResources().getString(R.string.cannot_play_podcast, status.getTitle());
        Toast.makeText(this, toastMessage, Toast.LENGTH_LONG).show();
      } else {
        QueueManager.changeActivePodcast(this, status.getPodcastId());
        _lockscreenManager.setupLockscreenControls(this, status);
        showNotification();
      }
    } else {
      _player.seekTo(status.getPosition());
    }
  }
Esempio n. 2
0
 private void prepareNextPodcast() {
   PlayerStatus currentState = PlayerStatus.getCurrentState(this);
   if (!currentState.hasActivePodcast()) {
     _player.stop();
   } else {
     _player.prepare(currentState.getFilename(), currentState.getPosition());
     _currentPodcastId = currentState.getPodcastId();
   }
 }
Esempio n. 3
0
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent == null || intent.getExtras() == null) return START_NOT_STICKY;

    if (!intent.getExtras().containsKey(Constants.EXTRA_PLAYER_COMMAND)) return START_NOT_STICKY;

    int pauseReason = intent.getIntExtra(Constants.EXTRA_PLAYER_COMMAND_ARG, -1);

    if (_player == null && intent.getExtras().containsKey(Constants.EXTRA_PLAYER_COMMAND))
      createPlayer();
    switch (intent.getIntExtra(Constants.EXTRA_PLAYER_COMMAND, -1)) {
      case -1:
        break;
      case Constants.PLAYER_COMMAND_PLAYPAUSE:
        _player.playPause(pauseReason);
        break;
      case Constants.PLAYER_COMMAND_PLAYSTOP:
        _player.playStop();
        break;
      case Constants.PLAYER_COMMAND_PLAY:
        _player.play();
        break;
      case Constants.PLAYER_COMMAND_PAUSE:
        _player.pause(pauseReason);
        break;
      case Constants.PLAYER_COMMAND_RESUME:
        _player.unpause(pauseReason);
        break;
      case Constants.PLAYER_COMMAND_STOP:
        _player.stop();
        break;
      case Constants.PLAYER_COMMAND_REFRESHPODCAST:
        refreshPodcast();
        break;
    }

    return START_NOT_STICKY;
  }
Esempio n. 4
0
  private void createPlayer() {
    if (_player == null) {
      _player = new PodcastPlayer(this);

      // handle errors so the onCompletionListener doens't get called
      _player.setOnErrorListener(
          new OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer player, int what, int extra) {
              String message =
                  String.format(Locale.US, "mediaplayer error - what: %d, extra: %d", what, extra);
              PodaxLog.log(PlayerService.this, message);
              return true;
            }
          });

      _player.setOnCompletionListener(
          new PodcastPlayer.OnCompletionListener() {
            @Override
            public void onCompletion() {
              QueueManager.moveToNextInQueue(PlayerService.this);
              prepareNextPodcast();
            }
          });

      _player.setOnPlayListener(
          new PodcastPlayer.OnPlayListener() {
            @Override
            public void onPlay(int duration) {
              // set this podcast as active
              ContentValues values = new ContentValues(1);
              if (duration > 0 && duration < 1000 * 60 * 60 * 6)
                values.put(PodcastProvider.COLUMN_DURATION, duration);
              values.put(PodcastProvider.COLUMN_ID, _currentPodcastId);
              getContentResolver().update(PodcastProvider.ACTIVE_PODCAST_URI, values, null, null);

              // listen for changes to the podcast
              getContentResolver()
                  .registerContentObserver(
                      PodcastProvider.PLAYER_UPDATE_URI, false, _podcastChangeObserver);

              // grab the media button
              AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
              ComponentName eventReceiver =
                  new ComponentName(PlayerService.this, MediaButtonIntentReceiver.class);
              audioManager.registerMediaButtonEventReceiver(eventReceiver);

              PlayerStatus.updateState(PlayerService.this, PlayerStates.PLAYING);
              _lockscreenManager.setLockscreenPlaying();
            }
          });

      _player.setOnPauseListener(
          new PodcastPlayer.OnPauseListener() {
            @Override
            public void onPause(int position) {
              updateActivePodcastPosition(position);
              PlayerStatus.updateState(PlayerService.this, PlayerStatus.PlayerStates.PAUSED);
              _lockscreenManager.setLockscreenPaused();
              showNotification();
            }
          });

      _player.setOnStopListener(
          new PodcastPlayer.OnStopListener() {
            @Override
            public void onStop(int position) {
              updateActivePodcastPosition(position);

              removeNotification();

              if (_lockscreenManager != null) _lockscreenManager.removeLockscreenControls();

              getContentResolver().unregisterContentObserver(_podcastChangeObserver);

              PlayerStatus.updateState(PlayerService.this, PlayerStatus.PlayerStates.STOPPED);

              stopSelf();
            }
          });

      _player.setOnSeekListener(
          new PodcastPlayer.OnSeekListener() {
            @Override
            public void onSeek(int position) {
              updateActivePodcastPosition(position);
            }
          });

      _player.setOnUnpreparedListener(
          new PodcastPlayer.OnUnpreparedListener() {
            @Override
            public void onUnprepared() {
              prepareNextPodcast();
            }
          });
    }

    if (_lockscreenManager == null) _lockscreenManager = new LockscreenManager();
  }