Introduction
The android media viewer mediaplayer video tutorial series describes how to use android’s mediaplayer to play videos from the MediaStore imager viewer.
Get Code
The code can be found on github from the following instructions below
https://github.com/mobapptuts/media-thumbnail-viewer.git Tag
media-viewer-mediaplayer
or you can run this command
git clone https://github.com/mobapptuts/media-thumbnail-viewer.git –branch
media-viewer-mediaplayer
This video describes how to import the code from github using android studio and also how to use git tags
Steps
Inside the MediaPlayer activity add members for the ImageButton, SurfaceView, MediaPlayer & video Uri
private MediaPlayer mMediaPlayer; private Uri mVideoUri; private ImageButton mPlayPauseButton; private SurfaceView mSurfaceView;
Now we are going to add some helper methods for playing & pausing
private void mediaPlay() { mMediaPlayer.start(); mImageButton.setImageResource(R.mipmap.ic_media_pause); } private void mediaPause() { mMediaPlayer.pause(); mImageButton.setImageResource(R.mipmap.ic_media_play); }
Setup initial configuration in the onCreate method
Attach to the layout views
mPlayPauseButton = (ImageButton) findViewById(R.id.videoPlayPauseButton); mSurfaceView = (SurfaceView) findViewById(R.id.videoSurfaceView);
Get the video Uri from the calling intent
Intent callingIntent = this.getIntent(); if(callingIntent != null) { mVideoUri = callingIntent.getData(); }
Setup the MediaPlayer
In the onResume method we check if we have an active MediaPlayer object. If so just start playing the video. If not, first we need to create a SurfaceHolder callback.
Inside the surfaceCreated callback method we can then create the MediaPlayer object with a valid SurfaceHolder.
public class VideoPlayActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnCompletionListener
@Override public void surfaceCreated(SurfaceHolder surfaceHolder) { mMediaPlayer = MediaPlayer.create(this, mVideoUri, surfaceHolder); mMediaPlayer.setOnCompletionListener(this); mediaPlay(); } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { }
@Override protected void onResume() { super.onResume(); if(mMediaPlayer != null) { mediaPlay(); } else { SurfaceHolder surfaceHolder = mSurfaceView.getHolder(); surfaceHolder.addCallback(this); } }
Implement the onPause and onStop methods
@Override protected void onStop() { if(mMediaPlayer != null) { mMediaPlayer.stop(); mMediaPlayer.release(); mMediaPlayer = null; } super.onStop(); } @Override protected void onPause() { super.onPause(); mediaPause(); }
Implement play and pause for the button click
public void playPauseClick(View view) { if(mMediaPlayer.isPlaying()) { mediaPause(); } else { mediaPlay(); } }
Add the MediaPlayer on completion listener
When the MediaPlayer stops playing we want to change the ImageButton icon to play.
@Override public void onCompletion(MediaPlayer mediaPlayer) { mPlayPauseButton.setImageResource(R.mipmap.ic_media_play); }
Inside the MediaStoreAdapter add a video uri method to the interface
public interface OnClickThumbListener { void OnClickImage(Uri imageUri); void OnClickVideo(Uri videoUri); }
Call the interface method when a video thumbnail is clicked
private void getOnClickUri(int position) { int mediaTypeIndex = mMediaStoreCursor.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE); int dataIndex = mMediaStoreCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA); mMediaStoreCursor.moveToPosition(position); String dataString = mMediaStoreCursor.getString(dataIndex); Uri mediaUri = Uri.parse("file://" + dataString); switch (mMediaStoreCursor.getInt(mediaTypeIndex)) { case MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE: mOnClickThumbListener.OnClickImage(mediaUri); break; case MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO: mOnClickThumbListener.OnClickVideo(mediaUri); break; default: } }
Implement the interface method in the main activity
@Override public void OnClickVideo(Uri videoUri) { Intent videoPlayIntent = new Intent(this, VideoPlayActivity.class); videoPlayIntent.setData(videoUri); startActivity(videoPlayIntent); }
Android media viewer mediaplayer video summary
Implementing the android MediaPlayer covered a number of things.
A initialised SurfaceHolder object was created through the SurfaceHolder callback.
That SurfaceHolder object was then used for the creation of the MediaPlayer.
Code was required for the various android states including pausing, stopping and responding to button clicks.
A MediaPlayer completion listener was also required to ensure the ImageButton’s play icon was set.