Пример #1
0
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean isKeyCodeSupported =
        keyCode != KeyEvent.KEYCODE_BACK
            && keyCode != KeyEvent.KEYCODE_VOLUME_UP
            && keyCode != KeyEvent.KEYCODE_VOLUME_DOWN
            && keyCode != KeyEvent.KEYCODE_MENU
            && keyCode != KeyEvent.KEYCODE_CALL
            && keyCode != KeyEvent.KEYCODE_ENDCALL;
    if (isInPlaybackState() && isKeyCodeSupported && mMediaController != null) {
      if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK
          || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
          || keyCode == KeyEvent.KEYCODE_SPACE) {
        if (mMediaPlayer.isPlaying()) {
          pause();
          mMediaController.show();
        } else {
          start();
          mMediaController.hide();
        }
        return true;
      } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP && mMediaPlayer.isPlaying()) {
        pause();
        mMediaController.show();
      } else {
        toggleMediaControlsVisiblity();
      }
    }

    return super.onKeyDown(keyCode, event);
  }
Пример #2
0
  private void openVideo() {
    if (mUri == null || mPlayer == null) return;

    mPlayer.reset();
    mInitialized = false;
    mPrepared = false;
    mVideoSizeKnown = false;

    try {
      mPlayer.setScreenOnWhilePlaying(true);
      mPlayer.setDataSource(PlayerService.this, mUri);
      // if (mLastAudioTrack != -1)
      // mPlayer.setInitialAudioTrack(mLastAudioTrack);
      // if (mLastSubTrackId != -1)
      // mPlayer.setInitialSubTrack(mLastSubTrackId);
      if (mSurfaceHolder != null
          && mSurfaceHolder.getSurface() != null
          && mSurfaceHolder.getSurface().isValid()) mPlayer.setDisplay(mSurfaceHolder);
      mPlayer.prepareAsync();
    } catch (IllegalArgumentException e) {
      Logger.e("openVideo", e);
    } catch (IllegalStateException e) {
      Logger.e("openVideo", e);
    } catch (IOException e) {
      Logger.e("openVideo", e);
    }
  }
Пример #3
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);
 }
Пример #4
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();
          }
        }
Пример #5
0
  private void initSizeSurfaceView() {
    /*int videoWidth = player.getVideoWidth();
    int videoHeight = player.getVideoHeight();
    Point screenSize = new Point();
    getWindowManager().getDefaultDisplay().getSize(screenSize);
    android.view.ViewGroup.LayoutParams lp = surfaceView.getLayoutParams();
    lp.width = screenSize.x;
    lp.height = (int) (((float)videoHeight / (float)videoWidth) * (float)screenSize.x);
    surfaceView.setLayoutParams(lp);*/

    int videoWidth = player.getVideoWidth();
    int videoHeight = player.getVideoHeight();
    float videoProportion = (float) videoWidth / (float) videoHeight;
    Point screenSize = new Point();
    getWindowManager().getDefaultDisplay().getSize(screenSize);
    float screenProportion = (float) screenSize.x / (float) screenSize.y;
    android.view.ViewGroup.LayoutParams lp = surfaceView.getLayoutParams();

    if (videoProportion > screenProportion) {
      lp.width = screenSize.x;
      lp.height = (int) ((float) screenSize.x / videoProportion);
    } else {
      lp.width = (int) (videoProportion * (float) screenSize.y);
      lp.height = screenSize.y;
    }
    surfaceView.setLayoutParams(lp);
  }
  private void fixMediaSize() {
    if (mMediaPlayer == null || !mIsMediaReadyToBePlayed) return;
    int width = mMediaPlayer.getVideoWidth();
    int height = mMediaPlayer.getVideoHeight();
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int screenWidth = size.x;
    int screenHeight = size.y;
    android.view.ViewGroup.LayoutParams videoParams = mMediaView.getLayoutParams();

    if (width > 0 && height > 0) {
      Log.i(TAG, "Video is: " + width + " x " + height);

      if (width > height) {
        videoParams.width = screenWidth;
        videoParams.height = screenWidth * height / width;
      } else {
        videoParams.width = screenHeight * width / height;
        videoParams.height = screenHeight;
      }
    } else {
      videoParams.width = 0;
      videoParams.height = 0;
    }

    Log.i(TAG, "Setting dimensions to: " + videoParams.width + " x " + videoParams.height);
    mMediaView.setLayoutParams(videoParams);
  }
Пример #7
0
 private void release(boolean cleartargetstate) {
   if (mMediaPlayer != null) {
     mMediaPlayer.reset();
     mMediaPlayer.release();
     mMediaPlayer = null;
     mCurrentState = STATE_IDLE;
     if (cleartargetstate) mTargetState = STATE_IDLE;
   }
 }
Пример #8
0
 public void stopPlayback() {
   if (mMediaPlayer != null) {
     mMediaPlayer.stop();
     mMediaPlayer.release();
     mMediaPlayer = null;
     mCurrentState = STATE_IDLE;
     mTargetState = STATE_IDLE;
   }
 }
Пример #9
0
 public void pause() {
   if (isInPlaybackState()) {
     if (mMediaPlayer.isPlaying()) {
       mMediaPlayer.pause();
       mCurrentState = STATE_PAUSED;
     }
   }
   mTargetState = STATE_PAUSED;
 }
 private void setupMedia() {
   doCleanUp();
   try {
     mMediaPlayer.setDataSource(extras.getString("mediaUrl"));
     mMediaPlayer.setDisplay(holder);
     mMediaPlayer.prepareAsync();
   } catch (Exception e) {
     Log.e(TAG, "error: " + e.getMessage(), e);
   }
 }
Пример #11
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;
        }
Пример #12
0
 private void preparePlayer() {
   if (player == null) {
     player = new MediaPlayer(this);
     playerNeedsPrepare = true;
     mediaController.setMediaPlayer(player);
     mediaController.setEnabled(true);
   }
   if (playerNeedsPrepare) {
     player.prepareAsync();
     playerNeedsPrepare = false;
   }
   player.setSurface(surfaceView.getHolder().getSurface());
   player.start();
 }
Пример #13
0
 // public int getSubLocation() {
 // if (mInitialized)
 // return mPlayer.getSubLocation();
 // return -1;
 // }
 //
 protected void setSubEncoding(String encoding) {
   if (mInitialized) {
     String enc = encoding.equals(VP.DEFAULT_SUB_ENCODING) ? null : encoding;
     mPlayer.setTimedTextEncoding(enc); // setSubEncoding ->
     // setTimedTextEncoding
   }
 }
Пример #14
0
 public void start() {
   if (isInPlaybackState()) {
     mMediaPlayer.start();
     mCurrentState = STATE_PLAYING;
   }
   mTargetState = STATE_PLAYING;
 }
 @Override
 public int getCurrentPosition() {
   if (mMediaPlayer != null && mIsMediaReadyToBePlayed) {
     return (int) mMediaPlayer.getCurrentPosition();
   } else {
     return -1;
   }
 }
 @Override
 public void seekTo(int i) {
   if (mMediaPlayer != null && mIsMediaReadyToBePlayed) {
     mMediaPlayer.seekTo(i);
   } else {
     Log.e(TAG, "MediaPlayer is not instantiated yet.");
   }
 }
 @Override
 public boolean isPlaying() {
   if (mMediaPlayer != null && mIsMediaReadyToBePlayed) {
     return mMediaPlayer.isPlaying();
   } else {
     return false;
   }
 }
Пример #18
0
 public void seekTo(long msec) {
   if (isInPlaybackState()) {
     mMediaPlayer.seekTo(msec);
     mSeekWhenPrepared = 0;
   } else {
     mSeekWhenPrepared = msec;
   }
 }
Пример #19
0
 @Override
 public boolean onInfo(MediaPlayer arg0, int arg1, int arg2) {
   switch (arg1) {
     case MediaPlayer.MEDIA_INFO_BUFFERING_START:
       if (mListener != null) mListener.onBufferStart();
       else mPlayer.pause();
       break;
     case MediaPlayer.MEDIA_INFO_BUFFERING_END:
       if (mListener != null) mListener.onBufferComplete();
       else mPlayer.start();
       break;
     case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:
       if (mListener != null) mListener.onDownloadRateChanged(arg2);
       break;
   }
   return true;
 }
Пример #20
0
 public long getDuration() {
   if (isInPlaybackState()) {
     if (mDuration > 0) return mDuration;
     mDuration = mMediaPlayer.getDuration();
     return mDuration;
   }
   mDuration = -1;
   return mDuration;
 }
Пример #21
0
 protected void setVolume(float left, float right) {
   if (mInitialized) {
     if (left <= 0f) left = 0f;
     else if (left >= 1f) left = 1f;
     if (right <= 0f) right = 0f;
     else if (right >= 1f) right = 1f;
     mPlayer.setVolume(left, right);
   }
 }
Пример #22
0
 @Override
 public void surfaceCreated(SurfaceHolder holder) {
   mSurfaceHolder = holder;
   if (mMediaPlayer != null
       && mCurrentState == STATE_SUSPEND
       && mTargetState == STATE_RESUME) {
     mMediaPlayer.setDisplay(mSurfaceHolder);
     resume();
   } else {
     openVideo();
   }
 }
 public void stop() {
   if (mMediaPlayer != null && mIsMediaReadyToBePlayed) {
     mMediaPlayer.stop();
     Intent intent = new Intent();
     intent.setAction(ACTION_INFO);
     intent.putExtra("action", "stop");
     if (!isStreaming) intent.putExtra("pos", getCurrentPosition());
     sendBroadcast(intent);
   } else {
     Log.e(TAG, "MediaPlayer is not instantiated yet.");
   }
 }
Пример #24
0
 private void release(boolean all) {
   if (mPlayer != null) {
     if (mListener != null) mListener.onCloseStart();
     mPlayer.reset();
     mInitialized = false;
     mPrepared = false;
     mVideoSizeKnown = false;
     if (mListener != null) mListener.onCloseComplete();
   }
   if (all) {
     mListener = null;
     mUri = null;
   }
 }
Пример #25
0
  private void playVideo() {
    doCleanUp();
    try {

      // Create a new media player and set the listeners
      player = new MediaPlayer(this);
      player.setDataSource(
          "http://htqjrpsv.cdnviet.com/htjzzsg/_definst_/vtv3.720p.stream/playlist.m3u8?t=b4781bdd2251d0e465e92824e9a11cf9&e=1428759643");
      player.setDisplay(holder);
      player.prepareAsync();
      player.setOnPreparedListener(this);
      player.setOnErrorListener(this);
      // player.setOnVideoSizeChangedListener(this);
      setVolumeControlStream(AudioManager.STREAM_MUSIC);
      mediaController.setMediaPlayer(player);
      mediaController.setEnabled(true);
      // player.setSurface(surfaceView.getHolder().getSurface());
      // player.start();
      mediaController.showLoading(0);

    } catch (Exception e) {
      Log.e(TAG, "error: " + e.getMessage(), e);
    }
  }
Пример #26
0
 private void vplayerInit(boolean isHWCodec) {
   mPlayer = new MediaPlayer(PlayerService.this.getApplicationContext(), isHWCodec);
   mPlayer.setOnHWRenderFailedListener(
       new OnHWRenderFailedListener() {
         @Override
         public void onFailed() {
           if (mListener != null) mListener.onHWRenderFailed();
         }
       });
   mPlayer.setOnBufferingUpdateListener(PlayerService.this);
   mPlayer.setOnCompletionListener(PlayerService.this);
   mPlayer.setOnPreparedListener(PlayerService.this);
   mPlayer.setOnVideoSizeChangedListener(PlayerService.this);
   mPlayer.setOnErrorListener(PlayerService.this);
   mPlayer.setOnInfoListener(PlayerService.this);
 }
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this)) return;

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow()
        .setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    extras = getIntent().getExtras();

    // handle extras
    if (extras == null) {
      wrapItUp(RESULT_CANCELED, "Error: No options provided");
    } else {
      if (extras.containsKey("isStreaming")) {
        isStreaming = extras.getBoolean("isStreaming");
      }

      if (extras.containsKey("shouldAutoClose")) {
        mShouldAutoClose = extras.getBoolean("shouldAutoClose");
      }

      mMediaType = extras.getString("type");
      if (mMediaType == null) mMediaType = MEDIA_TYPE_VIDEO;

      mMediaPlayer = new MediaPlayer(this);
      mMediaController = new MediaController(this, !isStreaming);
      mMediaController.setMediaPlayer(this);
      mMediaPlayer.setOnBufferingUpdateListener(this);
      mMediaPlayer.setOnCompletionListener(this);
      mMediaPlayer.setOnErrorListener(this);
      mMediaPlayer.setOnPreparedListener(this);
      mMediaPlayer.setOnVideoSizeChangedListener(this);
      setVolumeControlStream(AudioManager.STREAM_MUSIC);

      RelativeLayout relLayout = new RelativeLayout(this);

      if (extras.containsKey("bgColor")) {
        try {
          bgColor = Color.parseColor(extras.getString("bgColor"));
        } catch (Exception e) {
          Log.v(TAG, "Error parsing color");
          Log.e(TAG, e.toString());
          bgColor = DEFAULT_BG_COLOR;
        }
      }
      relLayout.setBackgroundColor(bgColor);

      RelativeLayout.LayoutParams relLayoutParam =
          new RelativeLayout.LayoutParams(
              RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
      relLayoutParam.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
      mMediaView = new SurfaceView(this);
      mMediaView.setLayoutParams(relLayoutParam);
      relLayout.addView(mMediaView);

      mProgressBar = new ProgressBar(this);
      mProgressBar.setIndeterminate(true);
      mProgressBar.setVisibility(View.VISIBLE);
      RelativeLayout.LayoutParams pblp =
          new RelativeLayout.LayoutParams(
              RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      pblp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
      mProgressBar.setLayoutParams(pblp);
      relLayout.addView(mProgressBar);
      mProgressBar.bringToFront();

      mMediaController.setAnchorView(relLayout);
      mMediaController.setEnabled(true);
      if (mMediaType.equalsIgnoreCase(MEDIA_TYPE_AUDIO)) {
        mMediaView.setBackgroundColor(bgColor);
        if (extras.containsKey("bgImage")) {
          if (extras.containsKey("bgImageScaleType")) {
            String scaleType = extras.getString("bgImageScaleType");
            if (scaleType.equalsIgnoreCase("fit")) {
              bgImageScaleType = ImageView.ScaleType.FIT_CENTER;
            } else if (scaleType.equalsIgnoreCase("stretch")) {
              bgImageScaleType = ImageView.ScaleType.FIT_XY;
            } else {
              bgImageScaleType = ImageView.ScaleType.CENTER;
            }
          }
          bgImage = new ImageView(this);
          new ImageLoadTask(extras.getString("bgImage"), this).execute(null, null);
          RelativeLayout.LayoutParams bgImageLayoutParam =
              new RelativeLayout.LayoutParams(
                  RelativeLayout.LayoutParams.MATCH_PARENT,
                  RelativeLayout.LayoutParams.MATCH_PARENT);
          bgImageLayoutParam.addRule(RelativeLayout.CENTER_IN_PARENT);
          bgImage.setLayoutParams(bgImageLayoutParam);
          bgImage.setScaleType(bgImageScaleType);
          relLayout.addView(bgImage);
        }
      }
      setContentView(relLayout, relLayoutParam);
      holder = mMediaView.getHolder();
      holder.addCallback(this);
      holder.setFormat(PixelFormat.RGBA_8888);
    }
  }
Пример #28
0
 public SparseArray<String> getSubTrackMap(String encoding) {
   if (mMediaPlayer != null)
     return mMediaPlayer.findTrackFromTrackInfo(
         TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT, mMediaPlayer.getTrackInfo(encoding));
   return null;
 }
Пример #29
0
 public int getTimedTextTrack() {
   if (mMediaPlayer != null) return mMediaPlayer.getTimedTextTrack();
   return -1;
 }
 private void releaseMediaPlayer() {
   if (mMediaPlayer != null) {
     mMediaPlayer.release();
     mMediaPlayer = null;
   }
 }