Android Open Source - AndroidVideoSamples Media Player Controller






From Project

Back to project page AndroidVideoSamples.

License

The source code is released under:

Apache License

If you think the Android project AndroidVideoSamples listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
   Copyright (c) 2014 Rory Hool// ww  w  .j  a  v  a2  s . c om
   
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
   
       http://www.apache.org/licenses/LICENSE-2.0
   
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 **/

package com.roryhool.videoplayback;

import java.io.IOException;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.view.Surface;

public class MediaPlayerController extends ControllerBase implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener {

   MediaPlayer mMediaPlayer;

   public MediaPlayerController( Context context ) {
      super( context );
   }

   @Override
   public void play() {
      if ( mMediaPlayer.getCurrentPosition() >= mMediaPlayer.getDuration() ) {
         mMediaPlayer.seekTo( 0 );
      }
      mMediaPlayer.start();
   }

   @Override
   public void pause() {
      mMediaPlayer.pause();

   }

   @Override
   public void seekTo( int ms ) {
      mMediaPlayer.seekTo( ms );
   }

   @Override
   public boolean isPlaying() {
      return mMediaPlayer.isPlaying();
   }

   @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() );
      }
      
      */
   }

   @Override
   public void onSurfaceTextureSizeChanged( SurfaceTexture surface, int width, int height ) {
   }

   @Override
   public boolean onSurfaceTextureDestroyed( SurfaceTexture surface ) {
      return false;
   }

   @Override
   public void onSurfaceTextureUpdated( SurfaceTexture surface ) {
   }

   @Override
   public int getDuration() {
      if ( mMediaPlayer != null ) {
         return mMediaPlayer.getDuration();
      }

      return 0;
   }

   @Override
   public int getCurrentPosition() {

      if ( mMediaPlayer != null ) {
         return mMediaPlayer.getCurrentPosition();
      }
      return 0;
   }

   @Override
   public int getVideoWidth() {

      if ( mMediaPlayer != null ) {
         return mMediaPlayer.getVideoWidth();
      }
      return 0;
   }

   @Override
   public int getVideoHeight() {

      if ( mMediaPlayer != null ) {
         return mMediaPlayer.getVideoHeight();
      }
      return 0;
   }

   @Override
   public void onVideoSizeChanged( MediaPlayer mediaPlayer, int width, int height ) {
      // TODO Auto-generated method stub

   }

   @Override
   public void onPrepared( MediaPlayer mediaPlayer ) {
      // TODO Auto-generated method stub

   }

   @Override
   public void onCompletion( MediaPlayer mediaPlayer ) {
      onCompletion();
      // showControls();
      // cancelTimer();
      // mPlayButton.setImageResource( R.drawable.ic_media_play );
   }

   @Override
   public void onBufferingUpdate( MediaPlayer mp, int percent ) {
      // TODO Auto-generated method stub

   }

}




Java Source Code List

com.roryhool.commonvideolibrary.CursorHelper.java
com.roryhool.commonvideolibrary.InputSurface.java
com.roryhool.commonvideolibrary.Intents.java
com.roryhool.commonvideolibrary.MediaHelper.java
com.roryhool.commonvideolibrary.OutputSurface.java
com.roryhool.commonvideolibrary.Resolution.java
com.roryhool.commonvideolibrary.SamplerClip.java
com.roryhool.commonvideolibrary.TextureRenderer.java
com.roryhool.commonvideolibrary.UriHelper.java
com.roryhool.commonvideolibrary.VideoResampler.java
com.roryhool.videocreation.JoinActivity.java
com.roryhool.videocreation.MainActivity.java
com.roryhool.videocreation.RenderFromSurfaceActivity.java
com.roryhool.videocreation.SurfaceEncoder.java
com.roryhool.videomanipulation.MainActivity.java
com.roryhool.videomanipulation.ResampleActivity.java
com.roryhool.videomanipulation.RotationActivity.java
com.roryhool.videomanipulation.TrimActivity.java
com.roryhool.videoplayback.ControllerBase.java
com.roryhool.videoplayback.DecodeWithMediaCodecActivity.java
com.roryhool.videoplayback.MainActivity.java
com.roryhool.videoplayback.MediaCodecDecodeController.java
com.roryhool.videoplayback.MediaPlayerController.java
com.roryhool.videoplayback.PlayWithMediaPlayerActivity.java
com.roryhool.videoplayback.PlaybackTimer.java
com.roryhool.videoplayback.ResizeAnimation.java
com.roryhool.videoplayback.ScaledTextureView.java
com.roryhool.videoplayback.VideoPlayerView.java