I have to use MediaPlayer to play a video, NOT a VideoView. I have a SurfaceView in Xml Layout and on the Activity side a MediaPlayer and a MediaController.
I coudldn't find a way to use MediaController with SurfaceView and MediaPlayer. It is so easy with VideoView but cant figure out how to use with MediaPlayer.
I have found examples online but they are all about playing audio, not video.
This is surfaceCreated method where i create the MediaPlayer
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(mSurfaceHolder);
mMediaPlayer.setDataSource(this, Uri.parse(mUrl));
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.prepare();
mMediaController = new MediaController(this);
} catch (Exception e) {
Log.e(TAG, "MediaPlayer Prepare: " + e.getMessage());
}
}
and this MediaPlayer onPrepared method
@Override
public void onPrepared(MediaPlayer mp) {
mVideoWidth = mp.getVideoWidth();
mVideoHeight = mp.getVideoHeight();
mSurfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
startVideoPlayback();
mMediaController.setMediaPlayer(this);
handler.post(new Runnable() {
public void run() {
mMediaController.setEnabled(true);
mMediaController.show();
}
});
}
The code above doesn't show the MediaController on SurfaceView.
How can i achieve this?
Thanks in advance.
for that you have to implement to activity
OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
OnVideoSizeChangedListener, SurfaceHolder.Callback, MediaPlayerControl
check below working code
package com.example.demo;
import android.app.Activity;
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.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.ProgressBar;
public class MainActivity extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
OnVideoSizeChangedListener, SurfaceHolder.Callback, MediaPlayerControl{
String url = "your video url";
MediaPlayer mMediaPlayer ;
SurfaceView mSurfaceView ;
SurfaceHolder holder ;
private ConstantAnchorMediaController controller = null;
ProgressBar progressBar1 ;
MediaController mcontroller ;
Handler handler ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
SurfaceView v = (SurfaceView) findViewById(R.id.surface);
handler = new Handler();
holder = v.getHolder();
holder.addCallback(this);
playVideo();
}
private void playVideo() {
try {
mcontroller = new MediaController(this);
mMediaPlayer = MediaPlayer.create(this, Uri.parse(url));
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mcontroller.show();
return false;
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
mMediaPlayer.setDisplay(holder);
try {
mMediaPlayer.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
progressBar1.setVisibility(View.GONE);
mcontroller.setMediaPlayer(this);
mcontroller.setAnchorView(findViewById(R.id.surface));
mcontroller.setEnabled(true);
handler.post(new Runnable() {
public void run() {
mcontroller.show();
}
});
}
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// TODO Auto-generated method stub
}
public void start() {
mMediaPlayer.start();
}
public void pause() {
mMediaPlayer.pause();
}
public int getDuration() {
return mMediaPlayer.getDuration();
}
public int getCurrentPosition() {
return mMediaPlayer.getCurrentPosition();
}
public void seekTo(int i) {
mMediaPlayer.seekTo(i);
}
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
public int getBufferPercentage() {
return 0;
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With