Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play two videos in one VideoView at the same time

This is not a question about playing two separate videos in two separate VideoViews on the one activity.

I have been asked to see if it is possible to create an activity with a single VideoView. When the user opens the Activity, they are asked to select a base video and then select a second video. Both videos will be playing in the one VideoView at the same time, but the base video will have an alpha of 255 and the second video will have an alpha of 150.

For testing though, video files located on the phone will do.

At this time, I have only been able to create an activity that plays a single video in a VideoView.

I thought if I created a custom VideoView class I could override the onDraw function and somehow grab the video frame from the second video, apply alpha and then redraw it over the first VideoView's canvas, but I do not know where to start.

My other concern with this process is the amount of memory used to play two videos at once in the one VideoView as well as the processing required to apply the alpha and then redraw it seamlessly without affecting the performance or playback of the video.

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

I'm developing a demo application to show the client on an Android 2.2 system using Eclipse. I'm not looking to target any higher systems at this time as the demo phone runs Android 2.2.

like image 393
Matthew Dally Avatar asked Nov 01 '22 17:11

Matthew Dally


2 Answers

I'm not entirely sure why you would want to use a VideoView like that. VideoViews use only one MediaPlayer and using it to sync one video on top of another would probably require a very kludgey implementation of two MediaPlayers through the same VideoView subclass, rendering on the same surface.

Take a look at the source code to see how a MediaPlayer renders a video inside of a VideoView and how MediaController controls playback. You can probably hack around in there to have two MediaPlayers point at once to the same VideoView/SurfaceView. Alternatively you could probably subclass MediaPlayer to handle multiple data sources.

Doing either of these things is counter to what VideoView and MediaPlayer are built for, and performance is going to take a huge hit.

If using a VideoView is not a hard requirement, then my suggestion would be to use an existing video library like ffmpeg, which would be easier and more performant than rewriting base media classes (caveat: using ffmpeg will require the NDK, I suggest using an existing ffmpeg wrapper to save time).

Once ffmpeg is added to your project, applying the secondary video as an OverlayVideoFilter would be fairly easy, and should allow you to layer one video on top of the other (though controlling playback simultaneously might be a challenge left for you).

The correct path to take probably depends on what you want to do with the compound video once you get it (export the video as a single video, control playback, etc.).

like image 67
Andrew Avatar answered Nov 15 '22 03:11

Andrew


Playing two videos in a single VideoView is not possible. This is because the VideoView is in reality an extended SurfaceView, which is both outdated, and never worked super well to begin with. (more on this at the bottom)

I don't know why you have a hard requirement on using a VideoView, as it is very simplistic, and will not give you what you need.

If your requirement for VideoView is because you want to keep the media controls and playback functionality, you're better off making a custom View. Extend LinearLayout and add two SurfaceViews to it with weights of 1. Copy the content of VideoView.java and place it in your new View, and make the modifications to handle two SurfaceViews playing two videos synchronously.

You're actually better off using TextureViews instead of SurfaceViews, which where added in api 14. It rectifies many of SurfaceView's shortcomings, and will handle things like animations better than the VideoView will.

like image 27
Doge Avatar answered Nov 15 '22 03:11

Doge