在CardView布局中运行媒体文件 [英] Run media file in CardView layout

查看:74
本文介绍了在CardView布局中运行媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在cardview布局中运行媒体文件(audio/video)?我想在card layout中具有文件的video预览,当用户单击卡片时,就像Facebook的feed一样,全屏显示并播放video/audio.

Is there a way to have a media file (audio/video) running in a cardview layout? I want to have a video preview of the file inside a card layout, when the user clicks on the card the video/audio is taken fullscreen and played, just like Facebook's feed.

推荐答案

为了获得这种功能,您必须解决一些问题:

In order to get that kind of functionality you have to solve a few problems:

  1. 您不能在列表中使用VideoView ,因为它正在扩展SurfaceView,并且如果打开播放并且用户开始滚动,则视觉效果看起来就像是视频正在尝试赶上滚动.因此,您需要基于其他内容的特殊VideoView,例如TextureView.

  1. You cannot use VideoView in a list because it is extending SurfaceView, and if playback is on and user starts scrolling, the visual effect will look like video is trying to catch the scroll. So you need a special VideoView that is based on something else, for example TextureView.

管理视频播放器状态: 为了开始/停止播放,我们需要调用MediaPlayer.class的一些方法:

Managing video player state: in order to start/stop playback we need to call a few methods of MediaPlayer.class:

setDataSource()
prepare()
start()
stop()
reset()
release()

这些方法是对硬件的直接调用,可能需要一些时间才能使硬件响应.因此,我们无法在UI线程中调用它.它将阻止它超过16毫秒,因此用户将看到滞后的滚动列表.我们必须从后台线程调用它.

These methods are direct calls to hardware and it may take some time before hardware will respond. So we cannot call it in UI thread. It will block it for more than 16 milliseconds, so user will see a lagging scrolled list. We have to call it from a background thread.

您需要在运行时跟踪屏幕上哪个视图处于活动状态,并且该视图应该正在播放.

You need to track in run time which view on the screen is active and this view should be playing.


我已经创建了一个名为 VideoPlayerManager 的开源项目.


I've created an opensource project called VideoPlayerManager.

它已被完全记录下来,并在演示应用程序中处理了一些基本流程.它尚未准备好投入生产,但是如果您需要一些有关如何获得这种功能的参考,可以在此处找到.

It is thoroughly documented and some basic flows are handled in the demo application. It is not ready for production but if you need some reference of how to get this kind of functionality, you can find it there.

P.S.关于CardView:这是常规视图.您可以将视频播放器放入其中,并在CardView中播放视频.

P.S. Regarding CardView: it is a regular view. You can put a video player into it and get video playback in CardView.

这篇关于在CardView布局中运行媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆