SurfaceView blank when playing a media player

This is my code, but when I start the media player, it only has sound and the surface is not displayed. Why?

I have no idea about this. You have some code to help me learn 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(); 
+6
source share
3 answers

Try adding after

 sh.addCallback(this); 

this is

 sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

In my case, it was helpful.

+5
source

Have you added a ready listener? I implemented a trigger in this method since it is a trigger that indicates when the video is ready for rendering.

 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(); } 
+3
source


try this code.

resource is the name of the file you want to play, and one.two is the name of the package, the path of which may look 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 check out this tutorial

+1
source

Source: https://habr.com/ru/post/892296/


All Articles