VideoPlayerDemo
封装ijkplay播放器, 自实现边下边播和缓存功能
Install / Use
/learn @Zhaoss/VideoPlayerDemoREADME
v1.1 增加本地视频播放功能, 视频会自动初始化宽高 不用手动传视频宽高了

本项目使用播放器是ijkplay, 并且进行封装和修改
主要功能:
1.重新编辑ijkplay的so库, 使其更精简和支持https协议
2.自定义MediaDataSource, 使用okhttp重写网络框架, 网络播放更流畅
3.实现视频缓存, 并且自定义LRUCache算法管理缓存文件
4.全局使用一个播放器, 实现视频在多个Activity之前无缝切换, 流畅播放
5.加入更多兼容性判断, 适配绝大数机型
①导入ijkplay:

//需要的权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
首先将lib文件夹下的so库粘贴过来, (因为官方自带的so库是不支持https的, 我重新编译的这个so库支持https协议,
并且使用的是精简版的配置, 网上关于ijkplay编译的流程和配置挺多的, 可以根据自己的需求自定义)
然后在module的build中加入 "implementation 'tv.danmaku.ijk.media:ijkplayer-java:0.8.8'"
②使用播放器的方法:
1.我封装了一个MediaPlayerTool工具类包含的初始化so库和一些回调等等
//通过单例得到媒体播放工具
mMediaPlayerTool = MediaPlayerTool.getInstance();
//这里会自动初始化so库 有些手机会找不到so, 会自动使用系统的播放器
private MediaPlayerTool(){
try {
IjkMediaPlayer.loadLibrariesOnce(null);
IjkMediaPlayer.native_profileBegin("libijkplayer.so");
loadIjkSucc = true;
}catch (UnsatisfiedLinkError e){
e.printStackTrace();
loadIjkSucc = false;
}
}
//一些生命周期回调
public static abstract class VideoListener {
//视频开始播放
public void onStart(){};
//视频被停止播放
public void onStop(){};
//视频播放完成
public void onCompletion(){};
//视频旋转角度参数初始化完成
public void onRotationInfo(int rotation){};
//播放进度 0-1
public void onPlayProgress(long currentPosition){};
//缓存速度 1-100
public void onBufferProgress(int progress){};
}
2.因为我使用的是RecyclerView,所以先找到当前屏幕中 处于可以播放范围的item
//首先循环RecyclerView中所有itemView, 找到在屏幕可见范围内的item
private void checkPlayVideo(){
currentPlayIndex = 0;
videoPositionList.clear();
int childCount = rv_video.getChildCount();
for (int x = 0; x < childCount; x++) {
View childView = rv_video.getChildAt(x);
//isPlayRange()这个方法很重要
boolean playRange = isPlayRange(childView.findViewById(R.id.rl_video), rv_video);
if(playRange){
int position = rv_video.getChildAdapterPosition(childView);
if(position>=0 && !videoPositionList.contains(position)){
videoPositionList.add(position);
}
}
}
}
//检查当前item是否在RecyclerView可见的范围内
private boolean isPlayRange(View childView, View parentView){
if(childView==null || parentView==null){
return false;
}
int[] childLocal = new int[2];
childView.getLocationOnScreen(childLocal);
int[] parentLocal = new int[2];
parentView.getLocationOnScreen(parentLocal);
boolean playRange = childLocal[1]>=parentLocal[1] &&
childLocal[1]<=parentLocal[1]+parentView.getHeight()-childView.getHeight();
return playRange;
}
3.我还封装了一个TextureView, 里面包含一些初始化SurfaceTexture和视频裁剪播放的方法
//视频居中播放
private void setVideoCenter(float viewWidth, float viewHeight, float videoWidth, float videoHeight){
Matrix matrix = new Matrix();
float sx = viewWidth/videoWidth;
float sy = viewHeight/videoHeight;
float maxScale = Math.max(sx, sy);
matrix.preTranslate((viewWidth - videoWidth) / 2, (viewHeight - videoHeight) / 2);
matrix.preScale(videoWidth/viewWidth, videoHeight/viewHeight);
matrix.postScale(maxScale, maxScale, viewWidth/2, viewHeight/2);
mTextureView.setTransform(matrix);
mTextureView.postInvalidate();
}
//初始化SurfaceTexture
public SurfaceTexture newSurfaceTexture(){
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
int texName = textures[0];
SurfaceTexture surfaceTexture = new SurfaceTexture(texName);
surfaceTexture.detachFromGLContext();
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 ou
Related Skills
docs-writer
99.3k`docs-writer` skill instructions As an expert technical writer and editor for the Gemini CLI project, you produce accurate, clear, and consistent documentation. When asked to write, edit, or revie
model-usage
338.0kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
ddd
Guía de Principios DDD para el Proyecto > 📚 Documento Complementario : Este documento define los principios y reglas de DDD. Para ver templates de código, ejemplos detallados y guías paso
zola-ai
An autonomous Solana wallet agent that executes payments via Twitter mentions and an in-app dashboard, powered by Claude.
