6

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 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();
Sergio
  • 28,539
  • 11
  • 85
  • 132
user829821
  • 563
  • 1
  • 6
  • 8

4 Answers4

5

Try to add after

sh.addCallback(this);

this

sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

In my case it was helpful.

Sirko
  • 72,589
  • 19
  • 149
  • 183
Gennady
  • 51
  • 1
  • 2
3

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();
}
DevProd
  • 573
  • 5
  • 6
1


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

Hussain
  • 5,552
  • 4
  • 40
  • 50
0

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.

Milad Yarmohammadi
  • 1,253
  • 2
  • 22
  • 37