Android how to use/show MediaController with SurfaceView and MediaPlayer for video?
14,363
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;
}
}
![Barbaros Alp](https://i.stack.imgur.com/IvUcn.jpg?s=256&g=1)
Comments
-
Barbaros Alp almost 2 years
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.
-
Lily over 10 yearsI tried this code but it displays application has stopped, I really need help with this please
-
Lily over 10 yearsIt does not start the application, it only displays application has stopped in the code side there are no errors displayed
-
Parag Chauhan over 10 years@Lily Download working demo - paragchauhan2010.blogspot.com/2013/10/…
-
Lily over 10 yearsI tried it and the same problem, the application does not start it just displays application has stopped. Any suggestions ?
-
Lily over 10 yearsThis is the message that appears on the log cat ": E/AndroidRuntime(20047): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mediaplayer_surf/com.example.mediaplayer_surf.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.mediaplayer_surf.MainActivity" on path: /data/app/com.example.mediaplayer_surf-2.apk" and then there are other messages which do not fit here
-
Parag Chauhan over 10 yearsClassNotFoundException error came ,may be you are declaring class with wrong package name
-
Lily over 10 yearsI mad sure of that more than once everything seems fine. Can you please provide me with the source code as an android application just to make sure the settings are right I tried downloading your source code but the zip file is not working probably thanks a lot for the help
-
Lily over 10 years
-
Parag Chauhan over 10 years