synchronized void setPlayerState(PlayerState playerState) {
    LOG.info(this.playerState.name() + " -> " + playerState.name() + " (" + currentPlaying + ")");

    if (playerState == PAUSED) {
      lifecycleSupport.serializeDownloadQueue();
    }

    Util.broadcastPlaybackStatusChange(this, playerState);

    this.playerState = playerState;

    if (playerState == STARTED) {
      scrobbler.scrobble(this, currentPlaying, false);
      NotificationUtil.setNotificationHiddenByUser(this, false);
    } else if (playerState == COMPLETED) {
      scrobbler.scrobble(this, currentPlaying, true);
    }

    MusicDirectory.Entry song = currentPlaying == null ? null : currentPlaying.getSong();
    NotificationUtil.updateNotification(this, this, handler, song, this.playerState == STARTED);
  }
 private void handleError(Exception x) {
   LOG.warn("Media player error: " + x, x);
   mediaPlayer.reset();
   setPlayerState(IDLE);
 }
  private synchronized void doPlay(final DownloadFile downloadFile, int position, boolean start) {
    try {
      final File file =
          downloadFile.isCompleteFileAvailable()
              ? downloadFile.getCompleteFile()
              : downloadFile.getPartialFile();
      downloadFile.updateModificationDate();
      mediaPlayer.setOnCompletionListener(null);
      mediaPlayer.reset();
      setPlayerState(IDLE);
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mediaPlayer.setDataSource(file.getPath());
      setPlayerState(PREPARING);
      mediaPlayer.prepare();
      setPlayerState(PREPARED);

      mediaPlayer.setOnCompletionListener(
          new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {

              // Acquire a temporary wakelock, since when we return from
              // this callback the MediaPlayer will release its wakelock
              // and allow the device to go to sleep.
              wakeLock.acquire(60000);

              setPlayerState(COMPLETED);

              // If COMPLETED and not playing partial file, we are *really" finished
              // with the song and can move on to the next.
              if (!file.equals(downloadFile.getPartialFile())) {
                onSongCompleted();
                return;
              }

              // If file is not completely downloaded, restart the playback from the current
              // position.
              int pos = mediaPlayer.getCurrentPosition();
              synchronized (DownloadServiceImpl.this) {

                // Work-around for apparent bug on certain phones: If close (less than ten seconds)
                // to the end
                // of the song, skip to the next rather than restarting it.
                Integer duration =
                    downloadFile.getSong().getDuration() == null
                        ? null
                        : downloadFile.getSong().getDuration() * 1000;
                if (duration != null) {
                  if (Math.abs(duration - pos) < 10000) {
                    LOG.info("Skipping restart from " + pos + " of " + duration);
                    onSongCompleted();
                    return;
                  }
                }

                LOG.info("Requesting restart from " + pos + " of " + duration);
                reset();
                bufferTask = new BufferTask(downloadFile, pos);
                bufferTask.start();
              }
            }
          });

      if (position != 0) {
        LOG.info("Restarting player from position " + position);
        mediaPlayer.seekTo(position);
      }

      if (start) {
        mediaPlayer.start();
        setPlayerState(STARTED);
      } else {
        setPlayerState(PAUSED);
      }
      lifecycleSupport.serializeDownloadQueue();

    } catch (Exception x) {
      handleError(x);
    }
  }