示例#1
0
 public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
   Log.d("onVideoSizeChanged: (%dx%d)", width, height);
   mVideoWidth = mp.getVideoWidth();
   mVideoHeight = mp.getVideoHeight();
   mVideoAspectRatio = mp.getVideoAspectRatio();
   if (mVideoWidth != 0 && mVideoHeight != 0) setVideoLayout(mVideoLayout, mAspectRatio);
 }
示例#2
0
        public void onPrepared(MediaPlayer mp) {
          Log.d("onPrepared");
          mCurrentState = STATE_PREPARED;
          mTargetState = STATE_PLAYING;

          if (mOnPreparedListener != null) mOnPreparedListener.onPrepared(mMediaPlayer);
          if (mMediaController != null) mMediaController.setEnabled(true);
          mVideoWidth = mp.getVideoWidth();
          mVideoHeight = mp.getVideoHeight();
          mVideoAspectRatio = mp.getVideoAspectRatio();

          long seekToPosition = mSeekWhenPrepared;

          if (seekToPosition != 0) seekTo(seekToPosition);
          if (mVideoWidth != 0 && mVideoHeight != 0) {
            setVideoLayout(mVideoLayout, mAspectRatio);
            if (mSurfaceWidth == mVideoWidth && mSurfaceHeight == mVideoHeight) {
              if (mTargetState == STATE_PLAYING) {
                start();
                if (mMediaController != null) mMediaController.show();
              } else if (!isPlaying() && (seekToPosition != 0 || getCurrentPosition() > 0)) {
                if (mMediaController != null) mMediaController.show(0);
              }
            }
          } else if (mTargetState == STATE_PLAYING) {
            start();
          }
        }
示例#3
0
        public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
          Log.d("Error: %d, %d", framework_err, impl_err);
          mCurrentState = STATE_ERROR;
          mTargetState = STATE_ERROR;
          if (mMediaController != null) mMediaController.hide();

          if (mOnErrorListener != null) {
            if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) return true;
          }

          if (getWindowToken() != null) {
            int message =
                framework_err == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK
                    ? R.string.VideoView_error_text_invalid_progressive_playback
                    : R.string.VideoView_error_text_unknown;

            new AlertDialog.Builder(mContext)
                .setTitle(R.string.VideoView_error_title)
                .setMessage(message)
                .setPositiveButton(
                    R.string.VideoView_error_button,
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                        if (mOnCompletionListener != null)
                          mOnCompletionListener.onCompletion(mMediaPlayer);
                      }
                    })
                .setCancelable(false)
                .show();
          }
          return true;
        }
示例#4
0
 public void suspend() {
   if (isInPlaybackState()) {
     release(false);
     mCurrentState = STATE_SUSPEND_UNSUPPORTED;
     Log.d("Unable to suspend video. Release MediaPlayer.");
   }
 }
示例#5
0
 public void onCompletion(MediaPlayer mp) {
   Log.d("onCompletion");
   mCurrentState = STATE_PLAYBACK_COMPLETED;
   mTargetState = STATE_PLAYBACK_COMPLETED;
   if (mMediaController != null) mMediaController.hide();
   if (mOnCompletionListener != null) mOnCompletionListener.onCompletion(mMediaPlayer);
 }
示例#6
0
        @Override
        public boolean onInfo(MediaPlayer mp, int what, int extra) {
          Log.d("onInfo: (%d, %d)", what, extra);
          if (mOnInfoListener != null) {
            mOnInfoListener.onInfo(mp, what, extra);
          } else if (mMediaPlayer != null) {
            if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) mMediaPlayer.pause();
            else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) mMediaPlayer.start();
          }

          return true;
        }
示例#7
0
  private void openVideo() {
    if (mUri == null || mSurfaceHolder == null || !Vitamio.isInitialized(mContext)) return;

    Intent i = new Intent("com.android.music.musicservicecommand");
    i.putExtra("command", "pause");
    mContext.sendBroadcast(i);

    release(false);
    try {
      mDuration = -1;
      mCurrentBufferPercentage = 0;
      mMediaPlayer = new MediaPlayer(mContext);
      mMediaPlayer.setOnPreparedListener(mPreparedListener);
      mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
      mMediaPlayer.setOnCompletionListener(mCompletionListener);
      mMediaPlayer.setOnErrorListener(mErrorListener);
      mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
      mMediaPlayer.setOnInfoListener(mInfoListener);
      mMediaPlayer.setOnSeekCompleteListener(mSeekCompleteListener);
      mMediaPlayer.setOnTimedTextListener(mTimedTextListener);
      mMediaPlayer.setDataSource(mContext, mUri);
      mMediaPlayer.setDisplay(mSurfaceHolder);
      mMediaPlayer.setScreenOnWhilePlaying(true);
      mMediaPlayer.prepareAsync();
      mCurrentState = STATE_PREPARING;
      attachMediaController();
    } catch (IOException ex) {
      Log.e("Unable to open content: " + mUri, ex);
      mCurrentState = STATE_ERROR;
      mTargetState = STATE_ERROR;
      mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
      return;
    } catch (IllegalArgumentException ex) {
      Log.e("Unable to open content: " + mUri, ex);
      mCurrentState = STATE_ERROR;
      mTargetState = STATE_ERROR;
      mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
      return;
    }
  }
示例#8
0
 public static int convertToInt(String str) throws NumberFormatException {
   int s, e;
   for (s = 0; s < str.length(); s++) if (Character.isDigit(str.charAt(s))) break;
   for (e = str.length(); e > 0; e--) if (Character.isDigit(str.charAt(e - 1))) break;
   if (e > s) {
     try {
       return Integer.parseInt(str.substring(s, e));
     } catch (NumberFormatException ex) {
       Log.e("convertToInt", ex);
       throw new NumberFormatException();
     }
   } else {
     throw new NumberFormatException();
   }
 }
示例#9
0
 /**
  * Set the display options
  *
  * @param layout
  *     <ul>
  *       <li>{@link #VIDEO_LAYOUT_ORIGIN}
  *       <li>{@link #VIDEO_LAYOUT_SCALE}
  *       <li>{@link #VIDEO_LAYOUT_STRETCH}
  *       <li>{@link #VIDEO_LAYOUT_ZOOM}
  *     </ul>
  *
  * @param aspectRatio video aspect ratio, will audo detect if 0.
  */
 public void setVideoLayout(int layout, float aspectRatio) {
   LayoutParams lp = getLayoutParams();
   DisplayMetrics disp = mContext.getResources().getDisplayMetrics();
   int windowWidth = disp.widthPixels, windowHeight = disp.heightPixels;
   float windowRatio = windowWidth / (float) windowHeight;
   float videoRatio = aspectRatio <= 0.01f ? mVideoAspectRatio : aspectRatio;
   mSurfaceHeight = mVideoHeight;
   mSurfaceWidth = mVideoWidth;
   if (VIDEO_LAYOUT_ORIGIN == layout
       && mSurfaceWidth < windowWidth
       && mSurfaceHeight < windowHeight) {
     lp.width = (int) (mSurfaceHeight * videoRatio);
     lp.height = mSurfaceHeight;
   } else if (layout == VIDEO_LAYOUT_ZOOM) {
     lp.width = windowRatio > videoRatio ? windowWidth : (int) (videoRatio * windowHeight);
     lp.height = windowRatio < videoRatio ? windowHeight : (int) (windowWidth / videoRatio);
   } else {
     boolean full = layout == VIDEO_LAYOUT_STRETCH;
     lp.width =
         (full || windowRatio < videoRatio) ? windowWidth : (int) (videoRatio * windowHeight);
     lp.height =
         (full || windowRatio > videoRatio) ? windowHeight : (int) (windowWidth / videoRatio);
   }
   setLayoutParams(lp);
   getHolder().setFixedSize(mSurfaceWidth, mSurfaceHeight);
   Log.d(
       "VIDEO: %dx%dx%f, Surface: %dx%d, LP: %dx%d, Window: %dx%dx%f",
       mVideoWidth,
       mVideoHeight,
       mVideoAspectRatio,
       mSurfaceWidth,
       mSurfaceHeight,
       lp.width,
       lp.height,
       windowWidth,
       windowHeight,
       windowRatio);
   mVideoLayout = layout;
   mAspectRatio = aspectRatio;
 }
示例#10
0
 @Override
 public void onTimedText(String text) {
   Log.i("onSubtitleUpdate: %s", text);
   if (mOnTimedTextListener != null) mOnTimedTextListener.onTimedText(text);
 }
示例#11
0
 @Override
 public void onTimedTextUpdate(byte[] pixels, int width, int height) {
   Log.i("onSubtitleUpdate: bitmap subtitle, %dx%d", width, height);
   if (mOnTimedTextListener != null)
     mOnTimedTextListener.onTimedTextUpdate(pixels, width, height);
 }
示例#12
0
 @Override
 public void onSeekComplete(MediaPlayer mp) {
   Log.d("onSeekComplete");
   if (mOnSeekCompleteListener != null) mOnSeekCompleteListener.onSeekComplete(mp);
 }