问题
Is it possible to render two video streams simultaneously, using different "sections" of a single SurfaceView
?
I have made sample code that renders two videos simultaneously using two SurfaceViews side-by-side, but I am wondering if it is possible to have both videos play on the same SurfaceView
.
Using a MediaPlayer
, you can set either the SurfaceHolder or the Surface itself as the display. I believe the code to achieve what I am asking would be inside of the surfaceCreated
method:
@Override
public void surfaceCreated(SurfaceHolder holder)
{
mediaPlayerTop.setDisplay(holder);
mediaPlayerBottom.setDisplay(holder);
play();
}
However, simply setting both MediaPlayer
s to the same Surface results in an IllegalStateException
when you try to prepare
the second MediaPlayer
(this is ignoring the fact that they'd probably overlap eachother anyways because I am not setting the position anywhere).
Basically, is what I am trying to achieve possible?
回答1:
Yes, but it takes some effort.
The basic plan is to direct the output of MediaPlayer to a SurfaceTexture, which converts each incoming frame to a GLES texture. You then render that to the SurfaceView, drawing a rect that fills half the view. You do the same thing for the other MediaPlayer.
The pieces you need can be found in Grafika, e.g. the "texture from camera" Activity takes a video stream from the camera preview, converts it to a GLES texture, and renders it to a SurfaceView.
Simply directing the output of two MediaPlayers to separate SurfaceViews is much easier, but less flexible.
Surfaces are endpoints in a producer-consumer pair. There can only be one producer at a time, so you can't simply direct two MediaPlayers at a single SurfaceView.
来源:https://stackoverflow.com/questions/36270328/is-it-possible-to-render-two-video-streams-simultaneously-on-a-single-surfacevie