Android视频/音频缓存框架AndroidVideoCache怎么用
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章主要为大家展示了"Android视频/音频缓存框架AndroidVideoCache怎么用",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Andro
千家信息网最后更新 2024年09月22日Android视频/音频缓存框架AndroidVideoCache怎么用
这篇文章主要为大家展示了"Android视频/音频缓存框架AndroidVideoCache怎么用",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Android视频/音频缓存框架AndroidVideoCache怎么用"这篇文章吧。
具体内容如下
对于视频/音频软件,音乐软件,视频软件,都有缓存这个功能,那如何实现边下边播功能:
如何实现这个边下边播功能?
文件是否支持同时读写?(Mediaplayer 播放文件,从网络上下载文件)
播放与下载进度如何协调?
已缓存的文件需及时清理
经过一番折腾,我 find 了 : [ AndroidVideoCache ],这个库是 danikula 大神写,看完源码后收益匪浅。实现流媒体边下边播原理利用socket 开启一个本机的代理服务器
结合自身需求,修改了该库,使用okhttp进行网络请求:
AndroidVideoCache (改成 okhttp 缓存)
package com.danikula.videocache;import android.text.TextUtils;import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InterruptedIOException;import java.util.Map;import java.util.concurrent.TimeUnit;import com.danikula.videocache.file.MyLog;import okhttp3.Call;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;import static com.danikula.videocache.ProxyCacheUtils.DEFAULT_BUFFER_SIZE;import static com.danikula.videocache.ProxyCacheUtils.LOG_TAG;import static java.net.HttpURLConnection.HTTP_OK;import static java.net.HttpURLConnection.HTTP_PARTIAL;/** * {@link Source} that uses http resource as source for {@link ProxyCache}. * * @author Alexey Danilov (danikula@gmail.com). * * 从URL 获取数据 */public class HttpUrlSource implements Source { private static final int MAX_REDIRECTS = 5; public final String url; private static OkHttpClient okHttpClient = new OkHttpClient(); private Call requestCall = null; private InputStream inputStream; private volatile int length = Integer.MIN_VALUE; private volatile String mime; private Mapheaders; public HttpUrlSource(String url) { this(url, ProxyCacheUtils.getSupposablyMime(url)); } public HttpUrlSource(String url, Map headers) { this(url, ProxyCacheUtils.getSupposablyMime(url)); this.headers = headers; } public HttpUrlSource(String url, String mime) { this.url = Preconditions.checkNotNull(url); this.mime = mime; } public HttpUrlSource(HttpUrlSource source) { this.url = source.url; this.mime = source.mime; this.length = source.length; } @Override public synchronized int length() throws ProxyCacheException { if (length == Integer.MIN_VALUE) { fetchContentInfo(); } return length; } @Override public void open(int offset) throws ProxyCacheException { try { Response response = openConnection(offset, -1); mime = response.header("Content-Type"); inputStream = new BufferedInputStream(response.body().byteStream(), DEFAULT_BUFFER_SIZE); length = readSourceAvailableBytes(response, offset, response.code()); } catch (IOException e) { throw new ProxyCacheException("Error opening okHttpClient for " + url + " with offset " + offset, e); } } private int readSourceAvailableBytes(Response response, int offset, int responseCode) throws IOException { int contentLength = Integer.valueOf(response.header("Content-Length", "-1")); return responseCode == HTTP_OK ? contentLength : responseCode == HTTP_PARTIAL ? contentLength + offset : length; } @Override public void close() throws ProxyCacheException { if (okHttpClient != null && inputStream != null && requestCall != null) { try { inputStream.close(); requestCall.cancel(); } catch (IOException e) { e.printStackTrace(); } } } @Override public int read(byte[] buffer) throws ProxyCacheException { if (inputStream == null) { throw new ProxyCacheException("Error reading data from " + url + ": okHttpClient is absent!"); } try { return inputStream.read(buffer, 0, buffer.length); } catch (InterruptedIOException e) { throw new InterruptedProxyCacheException("Reading source " + url + " is interrupted", e); } catch (IOException e) { throw new ProxyCacheException("Error reading data from " + url, e); } } private void fetchContentInfo() throws ProxyCacheException { MyLog.d(LOG_TAG, "Read content info from " + url); Response response = null; InputStream inputStream = null; try { response = openConnection(0, 20000); length = Integer.valueOf(response.header("Content-Length", "-1")); mime = response.header("Content-Type"); inputStream = response.body().byteStream(); MyLog.i(LOG_TAG, "Content info for `" + url + "`: mime: " + mime + ", content-length: " + length); } catch (IOException e) { MyLog.e(LOG_TAG, "Error fetching info from " + url, e); } finally { ProxyCacheUtils.close(inputStream); if (response != null) { requestCall.cancel(); } } } private Response openConnection(int offset, int timeout) throws IOException, ProxyCacheException { boolean redirected; int redirectCount = 0; String url = this.url; Request request = null; //do { MyLog.d(LOG_TAG, "Open okHttpClient " + (offset > 0 ? " with offset " + offset : "") + " to " + url);// okHttpClient = (HttpURLConnection) new URL(url).openConnection(); Request.Builder builder = new Request.Builder(); builder.url(url); //flac if(headers != null) { //设置请求头 for (Map.Entry entry : headers.entrySet()) { MyLog.i(LOG_TAG, "请求头信息 key:" + entry.getKey() +" Value" + entry.getValue());// okHttpClient.setRequestProperty(entry.getKey(), entry.getValue()); builder.addHeader(entry.getKey(), entry.getValue()); } } if (offset > 0) { builder.addHeader("Range", "bytes=" + offset + "-"); } request = builder.build(); requestCall = okHttpClient.newCall(request); /*if (redirected) { url = okHttpClient.getHeaderField("Location"); redirectCount++; okHttpClient.disconnect(); } if (redirectCount > MAX_REDIRECTS) { throw new ProxyCacheException("Too many redirects: " + redirectCount); }*/ //} while (redirected); return requestCall.execute(); } public synchronized String getMime() throws ProxyCacheException { if (TextUtils.isEmpty(mime)) { fetchContentInfo(); } return mime; } public String getUrl() { return url; } @Override public String toString() { return "HttpUrlSource{url='" + url + "}"; }}
以上是"Android视频/音频缓存框架AndroidVideoCache怎么用"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
缓存
视频
音频
内容
文件
框架
功能
篇文章
软件
网络
学习
帮助
上下
信息
原理
同时
大神
收益
数据
易懂
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
浅谈如何维护网络安全
网络安全会议会议纪要
查看数据库图表
深圳建行网络安全宣传周
厦门理工学院数据库系统考试
闵行区软件开发预算
技术服务合同与软件开发
网络安全法解读 在线阅读
怎么提升数据库安全性
服务器如何查看登录用户
政务服务中心网络安全管理
网络安全为主题的五年级手抄报
七年级信息技术网络安全
无线网络安全专家大会
我的世界一个很像hyp的服务器
吐鲁番软件开发规定
银行服务器的瓦数
在数据库敲打的代码怎样运行
云服务器 空间
网络安全蓝鲸事件
dns服务器地址最大值
扬州软件开发服务
数据库中表没有ui值是啥意思
DB服务器是
中小学生教育与网络安全回看
哪些服务器可以装备材质包
数据库的图形界面
云服务器挂载游戏
tp路由器设置数据库
都江堰市网络安全保卫大队