SurfaceView blank when play video usinng mediaplayer
This is My code but when I start the mediaplayer it have only the sound come out and the surface have nothing shown . Why?
I have no idea on开发者_JAVA百科 this. Do you have some code help me to learn with this.
videoV = (SurfaceView) findViewById(R.id.SurfaceView1);
sh = videoV.getHolder();
File path = Environment.getExternalStorageDirectory();
File file = new File(path, "sample2.mp4");
sh.addCallback(this);
mp = new MediaPlayer();
mp.setDataSource(file.getAbsolutePath());
mp.setDisplay(sh);
mp.prepare();
mp.start();
Try to add after
sh.addCallback(this);
this
sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
In my case it was helpful.
Have you added the on prepared listener? I implemented the start in that method since it is the trigger that indicates when the video is ready to be rendered.
sh.addCallback(this);
mp = new MediaPlayer();
mp.setDataSource(file.getAbsolutePath());
mp.setDisplay(sh);
mp.setOnPreparedListener(this);
mp.prepare();
public void onPrepared(MediaPlayer arg0) {
mp.start();
}
try this code.
resource
is file name which you want to play and one.two
is package name your path may as like android.resource://package_name/raw/file_name
VideoView video=(VideoView) findViewById(R.id.videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
//Uri uri = Uri.parse("android.resource://play.vedio/"+R.raw.dobeernotdrugs);
video.setKeepScreenOn(true);
video.setVideoPath("android.resource://one.two/raw/"+resource);
video.start();
video.requestFocus();
Also take a look at this tutorial
This could be beneficial for novice android developers or anyone who will see this.
In my case, using this snippet in OnCreate
method helped me to find out which device can use SurfaceView
if (
GLES20.glGetString(GLES20.GL_RENDERER) == null ||
GLES20.glGetString(GLES20.GL_VENDOR) == null ||
GLES20.glGetString(GLES20.GL_VERSION) == null ||
GLES20.glGetString(GLES20.GL_EXTENSIONS) == null ||
GLES10.glGetString(GLES10.GL_RENDERER) == null ||
GLES10.glGetString(GLES10.GL_VENDOR) == null ||
GLES10.glGetString(GLES10.GL_VERSION) == null ||
GLES10.glGetString(GLES10.GL_EXTENSIONS) == null) {
// try to use SurfaceView
} else {
// try to use TextureView
}
To find out differences between SurfaceView
and TextureView
see this link.
精彩评论