Exemplo n.º 1
0
 /**
  * Play the sound file currently selected in the file list. If there is no selection in the list,
  * or if the selected file is not a sound file, do nothing.
  */
 private void play() {
   String filename = (String) fileList.getSelectedValue();
   if (filename == null) { // nothing selected
     return;
   }
   // slider.setValue(0);
   boolean successful = player.play(new File(AUDIO_DIR, filename));
   if (successful) {
     showInfo(filename + " (" + player.getDuration() + " seconds)");
   } else {
     showInfo("Could not play file - unknown format");
   }
 }
  /** Handle a request to play music */
  private void handlePlayRequest() {
    LogHelper.d(TAG, "handlePlayRequest: mState=" + mPlayback.getState());

    mDelayedStopHandler.removeCallbacksAndMessages(null);
    if (!mServiceStarted) {
      LogHelper.v(TAG, "Starting service");
      // The MusicService needs to keep running even after the calling MediaBrowser
      // is disconnected. Call startService(Intent) and then stopSelf(..) when we no longer
      // need to play media.
      startService(new Intent(getApplicationContext(), MusicService.class));
      mServiceStarted = true;
    }

    if (!mSession.isActive()) {
      mSession.setActive(true);
    }

    if (QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
      updateMetadata();
      mPlayback.play(mPlayingQueue.get(mCurrentIndexOnQueue));
    }
  }
 /**
  * Helper to switch to a different Playback instance
  *
  * @param playback switch to this playback
  */
 private void switchToPlayer(Playback playback, boolean resumePlaying) {
   if (playback == null) {
     throw new IllegalArgumentException("Playback cannot be null");
   }
   // suspend the current one.
   int oldState = mPlayback.getState();
   int pos = mPlayback.getCurrentStreamPosition();
   String currentMediaId = mPlayback.getCurrentMediaId();
   LogHelper.d(TAG, "Current position from " + playback + " is ", pos);
   mPlayback.stop(false);
   playback.setCallback(this);
   playback.setCurrentStreamPosition(pos < 0 ? 0 : pos);
   playback.setCurrentMediaId(currentMediaId);
   playback.start();
   // finally swap the instance
   mPlayback = playback;
   switch (oldState) {
     case PlaybackState.STATE_BUFFERING:
     case PlaybackState.STATE_CONNECTING:
     case PlaybackState.STATE_PAUSED:
       mPlayback.pause();
       break;
     case PlaybackState.STATE_PLAYING:
       if (resumePlaying && QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
         mPlayback.play(mPlayingQueue.get(mCurrentIndexOnQueue));
       } else if (!resumePlaying) {
         mPlayback.pause();
       } else {
         mPlayback.stop(true);
       }
       break;
     case PlaybackState.STATE_NONE:
       break;
     default:
       LogHelper.d(TAG, "Default called. Old state is ", oldState);
   }
 }