oesTextureId = TextureHelper.createOESTextureObject()
surfaceTexture = SurfaceTexture(oesTextureId)
val surface = Surface(surfaceTexture)
mediaPlayer.setSurface(surface)
更新播放进度:
override fun onFrameAvailable(surfaceTexture: SurfaceTexture?) {
glSurfaceView.requestRender()
val position = mediaPlayer.currentPosition
videoSeekBar.progress = position
playTimeView.text = calculateTime(position)
默认情况下,绘制的视频会填满整个GLSurfaceView的大小,通常都会有视频播放拉伸的问题,因为视频的宽高比和容器的宽高比不一致,我们可以通过设置模型矩阵来调整GL顶点:
override fun onVideoSizeChanged(mp: MediaPlayer?, width: Int, height: Int) {
videoSeekBar.max = mediaPlayer.duration
sx = 1f * width / renderWidth
sy = 1f * height / renderHeight
override fun onDrawFrame(gl: GL10?) {
surfaceTexture?.updateTexImage()
surfaceTexture?.getTransformMatrix(transform)
Matrix.setIdentityM(mvpMatrix, 0)
Matrix.scaleM(mvpMatrix, 0, sx, sy, 1f)
cameraRender?.drawTexture(mvpMatrix, transform, oesTextureId)
MediaPlayer解出的视频帧转为纹理后,我们就可以通过GL操作纹理做很多有趣的东西了,比如添加水印,添加滤镜效果等,这里简单的将rgb转为gray
Gray = 0.2989 * R + 0.5870 * G + 0.1140 * B
完整的顶点着色器:
void main() {
vTextureCoord = (u_TextureMatrix * a_TextureCoordinate).xy;
gl_Position = a_Position * u_MvpMatrix;
片元着色器:
void main() {
vec4 color = texture2D(u_TextureSampler, vTextureCoord);
float gray = 0.299 * color.r + 0.587 * color.g + 0.114 * color.b;
vec4 newColor = vec4(gray, gray, gray, color.a);
gl_FragColor = mix(color, newColor, identity);
Demo中,在点击播放按钮的时候才开始更新GLSurfaceView,为了避免启动后界面一片黑,我们在GLSurfaceView上盖一层ImageView来展示一帧图像,开始播放后就隐藏这个ImageView,获取视频缩略图:
val mediaMetadataRetriever = MediaMetadataRetriever()
mediaMetadataRetriever.setDataSource(
assetFileDescriptor.fileDescriptor,
assetFileDescriptor.startOffset,
assetFileDescriptor.length
val bitmap = mediaMetadataRetriever.getFrameAtTime(0)
if (bitmap != null) {
videoThumbnailView.visibility = View.VISIBLE
videoThumbnailView.setImageBitmap(bitmap)
mediaMetadataRetriever.release()
https://github.com/sifutang/video.git
作者:雪月青
版权声明:本文内容转自互联网,本文观点仅代表作者本人。本站仅提供信息存储空间服务,所有权归原作者所有。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至[email protected] 举报,一经查实,本站将立刻删除。
赞
(0)
Coolpo 的 5 个优化工作空间的小贴士,让您无忧无虑地重返办公室
使用Python+OpenCV实现图像上轮廓绘制
测试 UCaaS 平台中的 Gen AI 用例,第 2 部分
Microsoft Teams 发布零售员工创新
在线视频仍然是内容投资的增长引擎,本地内容仍然是吸引订阅者的关键
SVT-AV1 2.1 发布,性能进一步提升
战略性打击密码共享,Netflix 受益匪浅
Gartner 2023 年企业对话式 AI 平台魔力象限
实验代码让开源 Mesa RADV Vulkan 驱动程序在 Windows 上运行
Zoom 达成 2,000 个座席的联络中心交易