Android - rendering same video on Multiple views

3.1k views Asked by At

I have a media datasource, and I am playing the media using android MediaPlayer.

How can I display the video output from the MediaPlayer to multiple views in the same Activity, is there any alternative ways to do this?

I want the video part of the media to be rendered in two different views without reading multiple times from data source.

Current Code :

TextureView mTextureView1;
TextureView mTextureView2;

mTextureView1.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            mMediaPlayer = new MediaPlayer(AppActivity.this);
            try {
                mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
            mMediaPlayer.setSurface(new Surface(surface));
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepareAsync();

            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });
        }
1

There are 1 answers

1
fadden On

You're currently playing into a TextureView, which receives the frames in a SurfaceTexture and then renders them onto the View UI layer.

One approach is to create your own SurfaceTexture to receive the frames, and then use OpenGL ES to render the frames however you like. For an example, see the "texture from camera" activity in Grafika. It can position, rotate, and scale the input from the camera; you can easily change this to receive input from the MediaPlayer, and render the texture twice onto a SurfaceView.

If you want to render it onto different Views, you could set up a couple of TextureViews and render to them. Use a single EGL context with a different EGL surface for each View.