MediaPlayer: 在不同控件之间实现视频的无缝切换的方法
最近使用MediaPlayer + TextureView 实现了一个视频播放器,并且实现了它的横竖屏切换的效果,唯一美中不足的是在横竖屏切换的时候画面会卡顿一下,虽然也不影响播放,但是怕测试会报Bug,到时候还得自己解决,所以就先把这个问题处理下,并记录之:
TextureView的监听方法有以下四个:
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) { } @Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) { } @Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return true;
} @Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { }
我们一般是在 onSurfaceTextureAvailable() 方法里面取得 SurfaceTexture 并包装成一个 Surface 再调用MediaPlayer的 setSurface 方法完成播放器的显示工作,然而在横竖屏切换的时候,由于当前TextureView所在的ViewGroup会把该TextureView remove掉,这里便触发了TextureView的 onSurfaceTextureDestroyed 方法,然后在横屏窗口中,由于需要重新添加TextureView以便于显示,所以这里又调用了 onSurfaceTextureAvailable 方法,我最开始的做法是这样的:
mSurface = new Surface(surfaceTexture);
然后传递给MediaPlayer, 由于在横竖屏切换时给MediaPlayer传递了两个不同的Surface,因此导致了这个问题,那么解决问题的方法也很简单,只要保存SurfaceTexture, 保证横竖屏切换时使用同一个Surface即可:
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
if (mSurfaceTexture == null) {
mSurfaceTexture = surfaceTexture;
openMediaPlayer();
} else {
mTextureView.setSurfaceTexture(mSurfaceTexture);
}
}
这里将SurfaceTexture保存为一个成员变量,在使用前对其进行判断,保证使用的是同一个SurfaceTexture,也可以在
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
Log.d(TAG, "onSurfaceTextureDestroyed: ");
mSurfaceTexture = surface;
return false;
}
也可以在 onSurfaceTextureDestroyed 方法中保存,不过不管在哪个方法中保存这个SurfaceTexture, 都需要注意 onSurfaceTextureDestroyed 方法的返回值必须为false, 官方API的解释说明如下:
Invoked when the specified SurfaceTexture is about to be destroyed. If returns true, no rendering should happen inside the surface texture after this method is invoked. If returns false, the client needs to call release(). Most applications should return true. 大致的意思是如果返回ture,SurfaceTexture会自动销毁,如果返回false,SurfaceTexture不会销毁,需要用户手动调用release()进行销毁。
- 切换至后台的时候会调用onSurfaceTextureDestroyed,从后台切换回来会调用onSurfaceTextureAvailable。
- TextureView的ViewGroup remove TextureView的时候会调用onSurfaceTextureDestroyed方法。相同,TextureView的ViewGroup add TextureView的时候会调用onSurfaceTextureAvailable。这些都是建立在视图可见的基础上,如果视图不可见,add也不会调用onSurfaceTextureAvailable方法,remove也不会调用onSurfaceTextureDestroyed方法。
- 当TextureView设置为Gone的时候,并不会调用onSurfaceTextureDestroyed方法法。
参考链接:
MediaPlayer: 在不同控件之间实现视频的无缝切换的方法的更多相关文章
- .NET同页面内用户控件与父页面以及控件之间方法调用
用户控件调用父页面的方法: //获得父页面 Page p =this.Parent.Page; Type pageType = p.GetType(); //父页面的方法名 MethodInfo mi ...
- 修复duilib CEditUI控件和CWebBrowserUI控件中按Tab键无法切换焦点的bug
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41556615 在duilib中,按tab键会让焦点在Button一类的控 ...
- sharepoint 2013 附件控件FileUpload怎样检验是否为图片的方法
记录一下关于附件控件FileUpload怎样检验是否为图片的方法: function checkImg() { var fileObj =document.getElementById('<%= ...
- C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法
C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法 1.新建组件这里可以自定义一个Panel控件起名为PanelEx 2.增加一个BoderColor属性和BoderSize属性 pr ...
- OLE、OCX和ActiveX控件之间的比较
OLE(Object Linking and Embedding,对象连接与嵌入) 一.过去的OLE和今天的OLE 最初的OLE含义是指在程序之间链接和嵌入对象数据,它提供了建立混合文档的手段(资 ...
- 【WPF】使用控件MediaElement播放视频
需求是点击按钮后,弹出弹窗播放视频.按钮的点击事件如下. public void ShowVideo() { Window window = new Window(); window.Width = ...
- android控件之间事件传递
public boolean dispatchTouchEvent(MotionEvent ev){} 用于事件的分发.Android中全部的事件都必须经过这种方法的分发.然后决定是自身消费当前事件还 ...
- WPF——控件之间的绑定
一.启动窗口 二.控件绑定(注意看光标的位置,一个是单向绑定,一个是双向绑定) 注意看单向绑定与双向绑定的绑定方法:
- WPF中获取控件之间的相对位置
1,获取元素相对于父控件的位置 使用Vector VisualTreeHelper.GetOffset(Visual visual)方法,其会返回visual在其父控件中的偏移量,然后你再将返回值的V ...
随机推荐
- 异常-Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException): Permission denied: user=hdfs, access=WRITE, inode="/hbase":root:supergroup:drwxr-xr-x
1 详细异常 Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlExce ...
- python高级特性-sorted()
1.数字排序 >>> sorted([1,-12,13,-4],key=abs) [1, -4, -12, 13] 2.字符串排序 按ASCII排序 默认情况下,对字符串排序,是按照 ...
- tensor flow 线性回归
# -*- coding: utf-8 -*-"""Spyder Editor This is a temporary script file.tensor flow 之 ...
- leetcode-2-重复的DNA序列
所有 DNA 都由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函 ...
- Notepad++ 连接远程 NppFTP
远程连接 1.自动安装: 插件——> Plugin Manager——>ShowPlugin Manager——>勾选NppFTP插件——>Install 2. 手动安装 ...
- java kafka
https://blog.csdn.net/panchang199266/article/details/82113453 安装部署 scala语言开发 + java
- cookie和session基础知识学习
一.session的简单使用 session是服务器端技术,服务器在运行时可以为每一个用户的浏览器创建一个独享的session对象.session的使用步骤: 获取session对象使用session ...
- 云计算(8)--MapReduce如何处理fault
一些常见的故障 NM周期性的给RM发送heartbeats,如果RM发现server fails,则它会让所有与这个server有关的AM知道,让受影响的job的AM采取一些action,重新分配它的 ...
- 人们为什么在Python脚本的第一行上编写#!/ usr / bin / env python shebang?
在我看来,如果没有该行,文件运行相同. #1楼 您可以使用virtualenv尝试此问题 这是test.py #! /usr/bin/env python import sys print(sys.v ...
- dell 7559 安装Manjaro 18
本来是装黑苹果的,折腾好几天都装好了,是可以正常使用的,可是clover始终有一个问题,每次启动前需要覆盖一遍EFI分区内EFI目录的CLOVER目录内的所有文件,方能引导MAC. 不然就卡Init ...