简体   繁体   English

同时在一个VideoView中播放两个视频

[英]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. 这不是关于在一个活动中在两个单独的VideoView中播放两个单独视频的问题。

I have been asked to see if it is possible to create an activity with a single VideoView. 我被要求查看是否可以使用单个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. 两个视频将同时在一个VideoView中播放,但基本视频的alpha值为255,第二个视频的alpha值为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. 目前,我只能创建一个在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. 我想如果我创建了一个自定义的VideoView类,我可以覆盖onDraw函数,并以某种方式从第二个视频中抓取视频帧,应用alpha然后在第一个VideoView的画布上重绘它,但我不知道从哪里开始。

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. 我对这个过程的另一个顾虑是用于在一个VideoView中一次播放两个视频的内存量,以及应用alpha然后无缝重绘它而不影响视频性能或播放所需的处理。

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. 我正在开发一个演示应用程序,使用Eclipse在Android 2.2系统上显示客户端。 I'm not looking to target any higher systems at this time as the demo phone runs Android 2.2. 由于演示手机运行的是Android 2.2,我现在不打算针对任何更高的系统。

I'm not entirely sure why you would want to use a VideoView like that. 我不完全确定你为什么要使用像这样的VideoView。 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. VideoViews只使用一个MediaPlayer,并使用它来同步一个视频,可能需要通过同一个VideoView子类在同一个表面上渲染两个MediaPlayers。

Take a look at the source code to see how a MediaPlayer renders a video inside of a VideoView and how MediaController controls playback. 查看源代码 ,了解MediaPlayer如何在VideoView中呈现视频以及MediaController如何控制播放。 You can probably hack around in there to have two MediaPlayers point at once to the same VideoView/SurfaceView. 你可以在那里乱砍,让两个MediaPlayers同时指向同一个VideoView / SurfaceView。 Alternatively you could probably subclass MediaPlayer to handle multiple data sources. 或者,您可以将MediaPlayer子类化为处理多个数据源。

Doing either of these things is counter to what VideoView and MediaPlayer are built for, and performance is going to take a huge hit. 执行这些操作中的任何一个都与构建VideoView和MediaPlayer相反,性能将受到巨大冲击。

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). 如果使用VideoView并不是一个硬性要求,那么我的建议是使用像ffmpeg这样的现有视频库,这比重写基础媒体类更容易,更高效(警告:使用ffmpeg将需要NDK,我建议使用现有的ffmpeg包装器以节省时间)。

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). 将ffmpeg添加到项目后,将辅助视频应用为OverlayVideoFilter将非常简单,并且应该允许您将一个视频叠加在另一个视频之上(尽管同时控制播放可能是一个挑战)。

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.). 正确的获取路径可能取决于您获得复合视频后要对其进行的操作(将视频导出为单个视频,控制播放等)。

Playing two videos in a single VideoView is not possible. 无法在单个VideoView中播放两个视频。 This is because the VideoView is in reality an extended SurfaceView, which is both outdated, and never worked super well to begin with. 这是因为VideoView实际上是一个扩展的SurfaceView,它已经过时了,并且开始时从未有过很好的工作。 (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. 我不知道为什么你对使用VideoView有严格的要求,因为它非常简单,并且不会满足你的需要。

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. 如果您对VideoView的要求是因为您希望保留媒体控制和播放功能,那么最好制作自定义视图。 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. 扩展LinearLayout并向其添加两个SurfaceViews,权重为1.复制VideoView.java的内容并将其放在新视图中,并进行修改以处理两个同步播放两个视频的SurfaceView。

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. 实际上你最好使用TextureViews而不是SurfaceViews,它们在api 14中添加。它可以纠正SurfaceView的许多缺点,并且可以比VideoView更好地处理动画等内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM