return surfaceTexture;
}
4.接下来就是播放代码了
private void playVideoByPosition(int position){
//根据传进来的 position 找到对应的 ViewHolder
final MainAdapter.MyViewHolder vh = (MainAdapter.MyViewHolder)
rv_video.findViewHolderForAdapterPosition(position);
if(vh == null){
return ;
}
currentPlayView = vh.rl_video;
//初始化一些播放状态, 如进度条,播放按钮,加载框等
//显示正在加载的界面
vh.iv_play_icon.setVisibility(View.GONE);
vh.pb_video.setVisibility(View.VISIBLE);
vh.iv_cover.setVisibility(View.VISIBLE);
vh.tv_play_time.setText(“”);
//初始化播放器
mMediaPlayerTool.initMediaPLayer();
mMediaPlayerTool.setVolume(0);
//设置视频 url
String videoUrl = dataList.get(position).getVideoUrl();
mMediaPlayerTool.setDataSource(videoUrl);
myVideoListener = new MediaPlayerTool.VideoListener() {
@Override
public void onStart() {
//将播放图标和封面隐藏
vh.iv_play_icon.setVisibility(View.GONE);
vh.pb_video.setVisibility(View.GONE);
//防止闪屏
vh.iv_cover.postDelayed(new Runnable() {
@Override
public void run() {
vh.iv_cover.setVisibility(View.GONE);
}
}, 300);
}
@Override
public void onStop() {
//播放停止
vh.pb_video.setVisibility(View.GONE);
vh.iv_cover.setVisibility(View.VISIBLE);
vh.iv_play_icon.setVisibility(View.VISIBLE);
vh.tv_play_time.setText(“”);
currentPlayView = null;
}
@Override
public void onCompletion() {
//播放下一个
currentPlayIndex++;
playVideoByPosition(-1);
}
@Override
public void onRotationInfo(int rotation) {
//设置旋转播放
vh.playTextureView.setRotation(rotation);
}
@Override
public void onPlayProgress(long currentPosition) {
//显示播放时长
String date = MyUtil.fromMMss(mMediaPlayerTool.getDuration() - currentPosition);
vh.tv_play_time.setText(date);
}
};
mMediaPlayerTool.setVideoListener(myVideoListener);
//这里重置一下 TextureView
vh.playTextureView.resetTextureView();
mMediaPlayerTool.setPlayTextureView(vh.playTextureView);
mMediaPlayerTool.setSurfaceTexture(vh.playTextureView.getSurfaceTexture());
//准备播放
mMediaPlayerTool.prepare();
}
③重写 MediaDataSource, 使用 okhttp 实现边下边播和视频缓存
1.一共需要重写 3 个方法 getSize(),close()和 readAt(); 先说 getSize()
public long getSize() throws IOException {
//开始播放时, 播放器会调用一下 getSize()来初始化视频大小, 这时我们就要初始化一条视频播放流
if(networkInPutStream == null) {
initInputStream();
}
return contentLength;
}
//初始化一个视频流出来, 可能是本地或网络
private void initInputStream() throws IOException{
File file = checkCache(mMd5);
if(file != null){
//更新一下缓存文件
VideoLRUCacheUtil.updateVideoCacheBean(mMd5, file.getAbsolutePath(), file.length());
//读取的本地缓存文件
isCacheVideo = true;
localVideoFile = file;
//开启一个本地视频流
localStream = new RandomAccessFile(localVideoFile, “rw”);
contentLength = file.length();
}else {
//没有缓存 开启一个网络流, 并且开启一个缓存流, 实现视频缓存
isCacheVideo = false;
//开启一个网络视频流
networkInPutStream = openHttpClient(0);
//要写入的本地缓存文件
localVideoFile = VideoLRUCacheUtil.createCacheFile(MyApplication.mContext, mMd5, contentLength);
//要写入的本地缓存视频流
localStream = new RandomAccessFile(localVideoFile, “rw”);
}
}
2.然后是 readAt()方法, 也是最重要的一个方法
/**
-
@param position 视频流读取进度
-
@param buffer 要把读取到的数据存到这个数组
-
@param offset 数据开始写入的坐标
-
@param size 本次一共读取数据的大小
-
@throws IOException
*/
//记录当前读取流的索引
long mPosition = 0;
@Override
public int readAt(long position, byte[] buffer, int offset, int size) throws IOException {
if(position>=contentLength || localStream==null){
return -1;
}
//是否将此字节缓存到本地
boolean isWriteVideo = syncInputStream(position);
//读取的流的长度不能大于 contentLength
if (position+size > contentLength) {
size -= position+size-contentLength;
}
//读取指定大小的视频数据
byte[] bytes;
if(isCacheVideo){
//从本地读取
bytes = readByteBySize(localStream, size);
}else{
//从网络读取
bytes = readByteBySize(networkInPutStream, size);
}
if(bytes != null) {
//写入到播放器的数组中
System.arraycopy(bytes, 0, buffer, offset, size);
if (isWriteVideo && !isCacheVideo) {
//将视频缓存到本地
localStream.write(bytes);
}
//记录数据流读取到哪步了
mPosition += size;
}
return size;
}
/**
- 从 inputStream 里读取 size 大小的数据
*/
private byte[] readByteBySize(InputStream inputStream, int size) throws IOException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[size];
int len;
while ((len = inputStream.read(buf)) != -1) {
out.write(buf, 0, len);
if (out.size() == size) {
return out.toByteArray();
} else {
buf = new byte[size - out.size()];
}
}
return null;
}
/**
- 删除 file 一部分字节, 从 position 到 file.size
*/
private void deleteFileByPosition(long position) throws IOException{
FileInputStream in = new FileInputStream(localVideoFile);
File tempFile = VideoLRUCacheUtil.createTempFile(MyApplication.mContext);
FileOutputStream out = new FileOutputStream(tempFile);
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) != -1) {
if(position <= len){
out.write(buf, 0, (int) position);
out.close();
in.close();
localVideoFile.delete();
tempFile.renameTo(localVideoFile);
localStream = new RandomAccessFile(localVideoFile, “rw”);
return ;
}else{
position -= len;
out.write(buf, 0, len);
}
}
tempFile.delete();
}
3.主要说一下 syncInputStream(), 因为有可能出现一种情况, 比如一个视频长度 100, 播放器首先读取视频的 1 到 10 之间的数据, 然后在读取 90 到 100 之间的数据, 然后在从 1 播放到 100; 所以这时我们需要同步视频流, 和播放进度保持一致这时就需要重新开启一个 IO 流(如果在读取本地缓存时可以直接使用 RandomAccessFile.seek()方法跳转)
//同步数据流
private boolean syncInputStream(long position) throws IOException{
boolean isWriteVideo = true;
//判断两次读取数据是否连续
if(mPosition != position){
if(isCacheVideo){
//如果是本地缓存, 直接跳转到该索引
localStream.seek(position);
}else{
if(mPosition > position){
//同步本地缓存流
localStream.close();
deleteFileByPosition(position);
localStream.seek(position);
}else{
isWriteVideo = false;
}
networkInPutStream.close();
//重新开启一个网络流
networkInPutStream = openHttpClient((int) position);
}
mPosition = position;
}
return isWriteVideo;
}
4.最后一个是 close()方法, 主要播放停止后释放一些资源
public void close() throws IOException {
if(networkInPutStream != null){
networkInPutStream.close();
networkInPutStream = null;
}
if(localStream != null){
localStream.close();
localStream = null;
}
if(localVideoFile.length()!=contentLength){
localVideoFile.delete();
}
}
④视频缓存和 LRUCache 管理
1.首先创建缓存文件, 在刚才的 MediaDataSource.getSize()方法里有一句代码
localVideoFile = VideoLRUCacheUtil.createCacheFile(MyApplication.mContext, mMd5, contentLength);
public static File createCacheFile(Context context, String md5, long fileSize){
//创建一个视频缓存文件, 在 data/data 目录下
File filesDir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File cacheFile = new File(filesDir, md5);
if(!cacheFile.exists()) {
cacheFile.createNewFile();
}
//将缓存信息存到数据库
VideoLRUCacheUtil.updateVideoCacheBean(md5, cacheFile.getAbsolutePath(), fileSize);
return cacheFile;
}
2.然后是读取缓存文件, 在刚才的 MediaDataSource.getSize()方法里还有一句代码
//检查本地是否有缓存, 2 步确认, 数据库中是否存在, 本地文件是否存在
private File checkCache(String md5){
//查询数据库
VideoCacheBean bean = VideoCacheDBUtil.query(md5);
if(bean != null){
File file = new File(bean.getVideoPath());
if(file.exists()){
return file;
}
}
return null;
}
3.LRUCache 的实现
//清理超过大小和存储时间的视频缓存文件
VideoLRUCacheUtil.checkCacheSize(mContext);
public static void checkCacheSize(Context context){
ArrayList videoCacheList = VideoCacheDBUtil.query();
//检查一下数据库里面的缓存文件是否存在
for (VideoCacheBean bean : videoCacheList){
if(bean.getFileSize() == 0){
File videoFile = new File(bean.getVideoPath());
//如果文件不存在或者文件大小不匹配, 那么删除
if(!videoFile.exists() && videoFile.length()!=bean.getFileSize()){
VideoCacheDBUtil.delete(bean);
}
}
}
long currentSize = 0;
long currentTime = System.currentTimeMillis();
for (VideoCacheBean bean : videoCacheList){
//太久远的文件删除
if(currentTime-bean.getPlayTime() > maxCacheTime){
VideoCacheDBUtil.delete(bean);
}else {
//大于存储空间的删除
if (currentSize + bean.getFileSize() > maxDirSize) {
VideoCacheDBUtil.delete(bean);
} else {
currentSize += bean.getFileSize();
}
}
}
//删除不符合规则的缓存
deleteDirRoom(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), VideoCacheDBUtil.query());
}
//更新缓存文件的播放次数和最后播放时间
public static void updateVideoCacheBean(String md5, String videoPath, long fileSize){
VideoCacheBean videoCacheBean = VideoCacheDBUtil.query(md5);
if(videoCacheBean == null){
videoCacheBean = new VideoCacheBean();
videoCacheBean.setKey(md5);
videoCacheBean.setVideoPath(videoPath);
videoCacheBean.setFileSize(fileSize);
}
videoCacheBean.setPlayCount(videoCacheBean.getPlayCount()+1);
videoCacheBean.setPlayTime(System.currentTimeMillis());
VideoCacheDBUtil.save(videoCacheBean);
}
⑤关于多个 Activity 同步播放状态, 无缝切换
1.首先在跳转时, 通知被覆盖的 activity 不关闭播放器
//首先跳转时通知一下 activity
mainActivity.jumpNotCloseMediaPlay(position);
文末
架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。
一线互联网Android面试题总结含详解(初级到高级专题)
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
}
videoCacheBean.setPlayCount(videoCacheBean.getPlayCount()+1);
videoCacheBean.setPlayTime(System.currentTimeMillis());
VideoCacheDBUtil.save(videoCacheBean);
}
⑤关于多个 Activity 同步播放状态, 无缝切换
1.首先在跳转时, 通知被覆盖的 activity 不关闭播放器
//首先跳转时通知一下 activity
mainActivity.jumpNotCloseMediaPlay(position);
文末
架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。
[外链图片转存中…(img-IKTBUW0h-1715364482092)]
[外链图片转存中…(img-nGyZeNpA-1715364482093)]
一线互联网Android面试题总结含详解(初级到高级专题)
[外链图片转存中…(img-rRegCRRZ-1715364482094)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!