Play two videos at the same time in one VideoView

It’s not a question of playing two separate videos in two separate video ads in one action.

I was asked to see if an action could be created using a single VideoView. When a user opens an event, they are prompted to select a base video, and then select a second video. Both videos will play in the same VideoView at the same time, but the base video will have alpha 255, and the second video will have alpha 150.

For testing, however, the video files located on the phone will be executed.

At this time, I was able to create only one video in VideoView.

I thought that if I created my own VideoView class, I could override the onDraw function and somehow capture the video frame from the second video, apply alpha, and then redraw it on top of the first VideoView canvas, but I don’t know where to start.

My other concern in this process is the amount of memory used to simultaneously play two videos in one VideoView, as well as the processing necessary to apply alpha and then redraw it without sacrificing performance or video playback.

I'm not sure where to start or how best to approach this and, if possible, was hoping for some guidance on using methods or objects.

I am developing a demo application to show a client on an Android 2.2 system using Eclipse. At the moment, I do not plan to target higher systems, since the demo phone is running Android 2.2.

+5
source share
2 answers

I'm not quite sure why you would like to use VideoView. VideoViews uses only one MediaPlayer, and using it to synchronize one video on top of another will probably require a very multi-user implementation of two MediaPlayers through the same subclass of VideoView, rendering on the same surface.

Check out the source code to see how MediaPlayer displays the video inside the VideoView and how MediaController controls playback. Perhaps you can hack in there so that two MediaPlayers point to the same VideoView / SurfaceView at once. Alternatively, you could subclass MediaPlayer to handle multiple data sources.

Doing any of these things goes against what VideoView and MediaPlayer are for, and performance will be a huge success.

If using VideoView is not a strict requirement, I would suggest using an existing video library such as ffmpeg , which is easier and more efficient than rewriting basic media classes (caveat: using ffmpeg will require NDK, I suggest using the existing ffmpeg wrapper to save time )

Once ffmpeg is added to your project, adding a secondary video like OverlayVideoFilter will be quite simple and should allow you to stack one video on top of another (although concurrent playback control may be a problem for you).

The right way to choose probably depends on what you want to do with the composite video as soon as you get it (export the video as a single video, control playback, etc.).

+1
source

It is not possible to play two videos in one VideoView. This is due to the fact that VideoView is actually an extended SurfaceView, which is deprecated and never worked from the very beginning. (more on this below)

I don’t know why you have a strict requirement to use VideoView, as it is very simplified and will not give you what you need.

If your requirement for VideoView is that you want to retain multimedia controls and playback features, you better create a custom view. Extend LinearLayout and add two SurfaceViews to them with weights 1. Copy the contents of VideoView.java and place it in your new view and make changes to handle two SurfaceViews that play two videos in sync.

Actually, you'd better use TextureView instead of SurfaceViews, which is added to api 14. It fixes many of the disadvantages of SurfaceView and will handle things like animations better than VideoView.

+1
source

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


All Articles