Ejemplo n.º 1
0
  public void startPlayback(float percentage) {
    if (state() == PLAYING_PAUSED_STATE) {
      mSampleStart = System.currentTimeMillis() - mPlayer.getCurrentPosition();
      mPlayer.seekTo((int) (percentage * mPlayer.getDuration()));
      mPlayer.start();
      setState(PLAYING_STATE);
    } else {
      stop();

      mPlayer = new MediaPlayer();
      try {
        mPlayer.setDataSource(mSampleFile.getAbsolutePath());
        mPlayer.setOnCompletionListener(this);
        mPlayer.setOnErrorListener(this);
        mPlayer.prepare();
        mPlayer.seekTo((int) (percentage * mPlayer.getDuration()));
        mPlayer.start();
      } catch (IllegalArgumentException e) {
        setError(INTERNAL_ERROR);
        mPlayer = null;
        return;
      } catch (IOException e) {
        setError(STORAGE_ACCESS_ERROR);
        mPlayer = null;
        return;
      }

      mSampleStart = System.currentTimeMillis();
      setState(PLAYING_STATE);
    }
  }
 public void seekTo(int pos) {
   if (pos < 0) {
     player.seekTo(0);
   } else if (pos > getDuration()) {
     player.seekTo(getDuration());
   } else {
     player.seekTo(pos);
   }
 }
Ejemplo n.º 3
0
  public static void combo() {

    if (scoreCounter >= 11 && comboTrigger <= 3) {
      incScore++;
      comboTrigger++;
      comboup.start();
      comboI1 = true;
      combovoice.start();
      scoreCounter = 0;
      thunder.seekTo(0);
      thunder.start();
      thunder.setLooping(true);
      combo2trig++;
    } else if (scoreCounter >= 11 && comboTrigger >= 3) {
      lightning = true;
      strike.seekTo(0);
      strike.start();
      strike.setLooping(true);
      scoreCounter = 0;
      comboI2 = false;
      comboI1 = false;

    } else if (scoreCounter < 11 && comboTrigger >= 3) {
      combodown.start();
      incScore = 1;
      scoreCounter = 0;
      comboTrigger = 0;
      lightning = false;
      comboI2 = false;
      comboI1 = false;
      combo2trig = 0;
      thunder.pause();
      strike.pause();
    } else if (scoreCounter < 11 && comboTrigger > 0) {
      combodown.start();
      incScore = 1;
      scoreCounter = 0;
      comboTrigger = 0;
      combo2trig = 0;
      lightning = false;
      comboI2 = false;
      comboI1 = false;
      thunder.pause();
      strike.pause();
    } else {
      comboI2 = false;
      comboI1 = false;
      incScore = 1;
      scoreCounter = 0;
      comboTrigger = 0;
      combo2trig = 0;
      lightning = false;
      thunder.pause();
      strike.pause();
    }
  }
Ejemplo n.º 4
0
 public void onPrepared(MediaPlayer mp) {
   // briefly show the mediacontroller
   mIsPrepared = true;
   if (mOnPreparedListener != null) {
     mOnPreparedListener.onPrepared(mMediaPlayer);
   }
   if (mMediaController != null) {
     mMediaController.setEnabled(true);
   }
   mVideoWidth = mp.getVideoWidth();
   mVideoHeight = mp.getVideoHeight();
   if (mVideoWidth != 0 && mVideoHeight != 0) {
     // Log.i("@@@@", "video size: " + mVideoWidth +"/"+
     // mVideoHeight);
     getHolder().setFixedSize(mVideoWidth, mVideoHeight);
     if (mSurfaceWidth == mVideoWidth && mSurfaceHeight == mVideoHeight) {
       // We didn't actually change the size (it was already at the
       // size
       // we need), so we won't get a "surface changed" callback,
       // so
       // start the video here instead of in the callback.
       if (mSeekWhenPrepared != 0) {
         mMediaPlayer.seekTo(mSeekWhenPrepared);
         mSeekWhenPrepared = 0;
       }
       if (mStartWhenPrepared) {
         mMediaPlayer.start();
         mStartWhenPrepared = false;
         if (mMediaController != null) {
           mMediaController.show();
         }
       } else if (!isPlaying() && (mSeekWhenPrepared != 0 || getCurrentPosition() > 0)) {
         if (mMediaController != null) {
           // Show the media controls when we're paused into a
           // video and make 'em stick.
           mMediaController.show(0);
         }
       }
     }
   } else {
     // We don't know the video size yet, but should start anyway.
     // The video size might be reported to us later.
     if (mSeekWhenPrepared != 0) {
       mMediaPlayer.seekTo(mSeekWhenPrepared);
       mSeekWhenPrepared = 0;
     }
     if (mStartWhenPrepared) {
       mMediaPlayer.start();
       mStartWhenPrepared = false;
     }
   }
 }
Ejemplo n.º 5
0
 public void seekTo(int paramInt) {
   if (paramInt >= 0) {
     int i = getDuration();
     if (paramInt <= i) mediaPlayer.seekTo(paramInt);
   } else if (paramInt < 0) mediaPlayer.seekTo(0);
   int j = getDuration();
   if (paramInt > j) {
     mediaPlayer.seekTo(getDuration());
   }
   if (!isPlaying) {
     start();
   }
 }
  /** Moves the movie to the requested seek position */
  public boolean seekTo(int position) {
    if (!isPlayableOnTexture()) {
      // DebugLog.LOGD("Cannot seek-to on this video since it is not on texture");
      return false;
    }

    if ((mCurrentState == MEDIA_STATE.NOT_READY) || (mCurrentState == MEDIA_STATE.ERROR)) {
      // DebugLog.LOGD("Cannot seek-to on this video if it is not ready");
      return false;
    }

    boolean result = false;
    mMediaPlayerLock.lock();
    if (mMediaPlayer != null) {
      try {
        mMediaPlayer.seekTo(position);
      } catch (Exception e) {
        mMediaPlayerLock.unlock();
        DebugLog.LOGE("Could not seek to position");
      }
      result = true;
    }
    mMediaPlayerLock.unlock();

    return result;
  }
Ejemplo n.º 7
0
 /**
  * Reconfigures MediaPlayer according to audio focus settings and starts/restarts it. This method
  * starts/restarts the MediaPlayer respecting the current audio focus state. So if we have focus,
  * it will play normally; if we don't have focus, it will either leave the MediaPlayer paused or
  * set it to a low volume, depending on what is allowed by the current focus settings. This method
  * assumes mPlayer != null, so if you are calling it, you have to do so from a context where you
  * are sure this is the case.
  */
 private void configMediaPlayerState() {
   LogHelper.d(TAG, "configMediaPlayerState. mAudioFocus=", mAudioFocus);
   if (mAudioFocus == AUDIO_NO_FOCUS_NO_DUCK) {
     // If we don't have audio focus and can't duck, we have to pause,
     if (mState == PlaybackStateCompat.STATE_PLAYING) {
       pause();
     }
   } else { // we have audio focus:
     if (mAudioFocus == AUDIO_NO_FOCUS_CAN_DUCK) {
       mMediaPlayer.setVolume(VOLUME_DUCK, VOLUME_DUCK); // we'll be relatively quiet
     } else {
       if (mMediaPlayer != null) {
         mMediaPlayer.setVolume(VOLUME_NORMAL, VOLUME_NORMAL); // we can be loud again
       } // else do something for remote client.
     }
     // If we were playing when we lost focus, we need to resume playing.
     if (mPlayOnFocusGain) {
       if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
         LogHelper.d(
             TAG, "configMediaPlayerState startMediaPlayer. seeking to ", mCurrentPosition);
         if (mCurrentPosition == mMediaPlayer.getCurrentPosition()) {
           mMediaPlayer.start();
           mState = PlaybackStateCompat.STATE_PLAYING;
         } else {
           mMediaPlayer.seekTo(mCurrentPosition);
           mState = PlaybackStateCompat.STATE_BUFFERING;
         }
       }
       mPlayOnFocusGain = false;
     }
   }
   if (mCallback != null) {
     mCallback.onPlaybackStatusChanged(mState);
   }
 }
 /** Seek to the given playback position (in ms) */
 @Override
 public void seekTo(int msec) throws IllegalStateException {
   Log.d(TAG, "seekTo()");
   if (mMediaPlayer != null) {
     mMediaPlayer.seekTo(msec);
   }
 }
Ejemplo n.º 9
0
 /** 暂停后继续播放 */
 public static void keepMusic() {
   if (musicSt && music != null) {
     music.seekTo(msec);
     music.start();
     music.setLooping(false);
   }
 }
Ejemplo n.º 10
0
 public void seekTo(int msec) {
   if (mMediaPlayer != null && mIsPrepared) {
     mMediaPlayer.seekTo(msec);
   } else {
     mSeekWhenPrepared = msec;
   }
 }
Ejemplo n.º 11
0
 private void tryToRestoreLocation() {
   try {
     if (location == null) {
       return;
     }
     boolean found = false;
     for (int i = 0; i < metaHolder.getSize(); i++) {
       if (metaHolder.get(i).getTitle().equals(location.title)) {
         currentPodcastInPlayer = i;
         found = true;
         break;
       }
     }
     if (!found) {
       location = null;
       return;
     }
     mediaPlayer.reset();
     mediaPlayer.setDataSource(currentFile().toString());
     mediaPlayer.prepare();
     mediaPlayer.seekTo(currentMeta().getCurrentPos());
     mediaMode = MediaMode.Paused;
   } catch (Throwable e) {
     // bummer.
   }
 }
 @Override
 public void play() {
   if (mMediaPlayer.getCurrentPosition() >= mMediaPlayer.getDuration()) {
     mMediaPlayer.seekTo(0);
   }
   mMediaPlayer.start();
 }
Ejemplo n.º 13
0
  public void setFileState(int state) {
    switch (state) {
      case 0:
        m_mediaPlayer.pause();
        break;

      case 1:
        m_mediaPlayer.stop();
        try {
          m_mediaPlayer.prepare();
        } catch (IllegalStateException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
        m_mediaPlayer.seekTo(0);
        break;

      case 2:
      case 3:
        m_mediaPlayer.start();
        break;

      default:
        break;
    }
  }
Ejemplo n.º 14
0
  @Override
  public void onPrepared(MediaPlayer mp) {

    if (isPrepare) {
      if (playType == PLAY_TYPE_LOD || playType == PLAY_TYPE_ONDEMAND) {
        // TODO 数据库操作 记忆续播
        MPConPlayManager cPm =
            MPConPlayUtils.findConnectionPlay(currentPosition, parentName, getChildName());
        if (cPm != null && cPm.getEpisodePos() == currentPosition && cPm.getPlayIndex() != 0) {
          mp.seekTo(cPm.getPlayIndex());
        }

        mHandler.sendEmptyMessage(SHOW_CONTROL_ANIMATION);
      }

      mp.start();
      isPrepare = false;

      if (mDialog != null && mDialog.isShowing()) {
        mDialog.dismiss();
      }
      // 判断是否是本地播放或在线点播
      if (playType == PLAY_TYPE_LOD || playType == PLAY_TYPE_ONDEMAND) {
        if (mp.isPlaying() && !mSeekBar.isPressed()) {

          mHandler.sendEmptyMessage(UPDATA_PLAY_TIME);
        }
      }

      mProgressBar.setVisibility(View.INVISIBLE);

      mPlayer.setScreenOnWhilePlaying(true);
    }
  }
Ejemplo n.º 15
0
 public void play() {
   if (mMediaPlayer.isPlaying()) {
     mMediaPlayer.seekTo(0);
   } else {
     mMediaPlayer.start();
   }
 }
Ejemplo n.º 16
0
 @Override
 public void seekTo(int msec) throws IllegalStateException {
   if (isReady()) {
     super.seekTo(msec);
     pauseTime = msec;
   }
 }
Ejemplo n.º 17
0
 /** Seeks to 0 time position. */
 public void seekToStart() {
   if (mediaPlayer != null && isPrepared) {
     mediaPlayer.seekTo(0);
   } else {
     Log.e(Player.class.getSimpleName(), "Couldn't seek to start");
   }
 }
Ejemplo n.º 18
0
 /**
  * Start preparing plauer for play. After call this method player is preparing. If player is
  * prepared then start playing. If player is prepared and method is called again with this same
  * parameter then player starts playing immediately form beginning.
  *
  * @return Flag that indicate the Player need to preparing.
  */
 public boolean play(String path) {
   if (this.sourceUrl.equals(path)) {
     if (mediaPlayer == null) {
       setDataSource(path);
     } else if (isPrepared) {
       if (isPlaying()) {
         mediaPlayer.seekTo(0);
       } else {
         mediaPlayer.start();
       }
       return true;
     } else {
       if (endListener != null) {
         endListener.onCompletion(mediaPlayer);
       }
       setDataSource(path);
     }
   } else {
     if (endListener != null) {
       endListener.onCompletion(mediaPlayer);
     }
     release();
     setDataSource(path);
   }
   return false;
 }
Ejemplo n.º 19
0
  private void _seek(int msec) {
    Log.v(TAG, "seekTo:" + msec);

    int runningDuration = 0;
    int targetIndex = 0;
    for (int i = 0; i < durations.size(); ++i) {
      int dur = durations.get(i);
      targetIndex = i;

      if (runningDuration + dur > msec) {
        break;
      }

      runningDuration += dur;
    }

    currentIndex = targetIndex;

    int newPos = msec - runningDuration;
    Log.v(TAG, "newPos:" + newPos);

    stop();
    prepare(currentIndex);
    mediaPlayer.seekTo(newPos);
  }
Ejemplo n.º 20
0
  /** This method runs at a fixed rate. */
  @Override
  public void run() {
    switch (mState) {
      case PREPARED:
        mState = BellRepeaterState.PLAYING;
        mMediaPlayer.start();
        Log.i("BellRepeater", "Media player starting");
        break;
      case PLAYING:
        // Restart the tone
        // mState remains PLAYING
        mMediaPlayer.seekTo(0);
        Log.i("BellRepeater", "Media player restarting");
        break;
      case STOPPED:
        // In theory this shouldn't happen, because the timer should be cancelled.
        // But just in case, do nothing.
        return;
      default:
        break;
    }

    // If it's not the last repetition, set the completion listener to change the state to
    // PREPARED, so that on the next run() we know to use start() rather than seekTo().
    if (++mRepetitionsSoFar < mBellInfo.getTimesToPlay()) {
      mMediaPlayer.setOnCompletionListener(
          new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
              mState = BellRepeaterState.PREPARED;
              Log.i("BellRepeater", "Media player completed");
            }
          });

      // If it's the last repetition, set the completion listener to release the player and
      // change the state to FINISHED, and cancel the timer (i.e. clean everything up).
    } else {
      mMediaPlayer.setOnCompletionListener(
          new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
              // The MediaPlayer coming here should be the same one as mMediaPlayer in the
              // BellRepeater class
              if (mp != mMediaPlayer) {
                Log.e(
                    this.getClass().getSimpleName(),
                    "OnCompletionListener mp wasn't the same as mMediaPlayer!");
              }
              mMediaPlayer.release();
              mMediaPlayer = null;
              mState = BellRepeaterState.FINISHED;
              Log.i("BellRepeater", "Over and out");
            }
          });

      mTimer.cancel();
    }
  }
Ejemplo n.º 21
0
        public void onPrepared(MediaPlayer mp) {
          // TODO Auto-generated method stub

          try {
            msurfaceholder.setFixedSize(getPictureWidth(), getPictureHeight());

            int progress = 0;
            progress = getSavedProgress();
            if (progress > 0) {
              mmediaplayer.seekTo(progress);
              sendMsg(VideoConst.MSG_SEEKING);
              mPlayStatus = VideoConst.PLAY_STATUS_SEEKING;
            } else {
              mmediaplayer.start();
              sendMsg(VideoConst.MSG_PLAY_START);
              mPlayStatus = VideoConst.PLAY_STATUS_STARTED;
              if (mOnPBPreparedListener != null) {
                mOnPBPreparedListener.onPrepared(pb);
              }
            }
            if (previewMode == true) {
              setPreviewRect();
            }
          } catch (Exception e) {
            try {
              throw new Exception(e);
            } catch (Exception e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
            }
          }
        }
Ejemplo n.º 22
0
 private void seekTo(int position) {
   player.seekTo(position);
   if (isPause) {
     player.start();
     isPause = false;
   }
 }
Ejemplo n.º 23
0
  private void prevTrack() {
    try {
      if (player != null) {
        int time = player.getCurrentPosition();
        if (time >= 3000) {
          player.seekTo(0);
          return;
        }
      }

      currentIndex--;
      if (currentIndex < 0) {
        currentIndex = trackIds.length - 1;
      }

      prepareToPlay(trackIds[currentIndex]);

    } finally {
      postProcesses.add(
          new Runnable() {
            @Override
            public void run() {
              playTrack();
              eventBus.post(new TrackChangedEvent(currentIndex));
            }
          });
    }
  }
  @Override
  public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
    Surface s = new Surface(surfaceTexture);

    try {
      mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setDataSource(mContext, mVideoUri);
      mMediaPlayer.setSurface(s);
      mMediaPlayer.prepare();
      mMediaPlayer.setOnBufferingUpdateListener(this);
      mMediaPlayer.setOnCompletionListener(this);
      mMediaPlayer.setOnPreparedListener(this);
      mMediaPlayer.setOnVideoSizeChangedListener(this);
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mMediaPlayer.seekTo(0);
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    /*
    if ( mMediaPlayer != null ) {
       mSeekBar.setProgress( 0 );
       mSeekBar.setMax( mMediaPlayer.getDuration() );
       mVideoTextureView.SetVideoSize( mMediaPlayer.getVideoWidth(), mMediaPlayer.getVideoHeight() );
    }

    */
  }
Ejemplo n.º 25
0
 public void seekAudio(int seekTo) {
   if ((mAudioPlayer != null) && mIsPrepared) {
     mAudioPlayer.seekTo(seekTo);
   } else {
     mSeekWhenPrepared = seekTo;
   }
 }
  // initializes the SlideshowPlayer Activity
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.slideshow_player);

    imageView = (ImageView) findViewById(R.id.imageView);
    videoView = (VideoView) findViewById(R.id.videoView);
    videoView.setOnCompletionListener( // set video completion handler
        new OnCompletionListener() {
          @Override
          public void onCompletion(MediaPlayer mp) {
            handler.post(updateSlideshow); // update the slideshow
          } // end method onCompletion
        } // end anonymous inner class
        ); // end OnCompletionListener

    if (savedInstanceState == null) // Activity starting
    {
      // get slideshow name from Intent's extras
      slideshowName = getIntent().getStringExtra(Slideshow.NAME_EXTRA);
      mediaTime = 0; // position in media clip
      nextItemIndex = 0; // start from first image
    } // end if
    else // Activity resuming
    {
      // get the play position that was saved when config changed
      mediaTime = savedInstanceState.getInt(MEDIA_TIME);

      // get index of image that was displayed when config changed
      nextItemIndex = savedInstanceState.getInt(IMAGE_INDEX);

      // get name of slideshow that was playing when config changed
      slideshowName = savedInstanceState.getString(SLIDESHOW_NAME);
    } // end else

    // get SlideshowInfo for slideshow to play
    slideshow = Slideshow.getSlideshowInfo(slideshowName);

    // configure BitmapFactory.Options for loading images
    options = new BitmapFactory.Options();
    options.inSampleSize = 4; // sample at 1/4 original width/height

    // if there is music to play
    if (slideshow.getMusicPath() != null) {
      // try to create a MediaPlayer to play the music
      try {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(this, Uri.parse(slideshow.getMusicPath()));
        mediaPlayer.prepare(); // prepare the MediaPlayer to play
        mediaPlayer.setLooping(true); // loop the music
        mediaPlayer.seekTo(mediaTime); // seek to mediaTime
      } // end try
      catch (Exception e) {
        Log.v(TAG, e.toString());
      } // end catch
    } // end if

    handler = new Handler(); // create handler to control slideshow
  } // end method onCreate
Ejemplo n.º 27
0
 public void seekTo(int msec) {
   if (isInPlaybackState()) {
     mMediaPlayer.seekTo(msec);
     mSeekWhenPrepared = 0;
   } else {
     mSeekWhenPrepared = msec;
   }
 }
Ejemplo n.º 28
0
  public void userSeekTo(int _time) {

    if (mPlayer != null && mIsPrepared) {

      mPlayer.pause();
      mPlayer.seekTo(_time);
    }
  }
Ejemplo n.º 29
0
  private void Rewind(int milisecond) {
    int position = mediaPlayer.getCurrentPosition();
    position -= milisecond;

    if (position < 1) position = 0;

    mediaPlayer.seekTo(position);
  }
Ejemplo n.º 30
0
 public void playCoo() {
   if (!coo.isPlaying()) {
     if (coo.getCurrentPosition() != 1) {
       coo.seekTo(1);
     }
     coo.start();
   }
 }