Android視頻/音頻緩存框架AndroidVideoCache(Okhttp)詳解
關(guān)于安卓邊下邊播功能,供大家參考,具體內(nèi)容如下
對于視頻/音頻軟件,音樂軟件,視頻軟件,都有緩存這個(gè)功能,那如何實(shí)現(xiàn)邊下邊播功能:
- 如何實(shí)現(xiàn)這個(gè)邊下邊播功能?
- 文件是否支持同時(shí)讀寫?(Mediaplayer 播放文件,從網(wǎng)絡(luò)上下載文件)
- 播放與下載進(jìn)度如何協(xié)調(diào)?
- 已緩存的文件需及時(shí)清理
經(jīng)過一番折騰,我 find 了 : [ AndroidVideoCache ],這個(gè)庫是 danikula 大神寫,看完源碼后收益匪淺。實(shí)現(xiàn)流媒體邊下邊播原理利用socket 開啟一個(gè)本機(jī)的代理服務(wù)器
結(jié)合自身需求,修改了該庫,使用okhttp進(jìn)行網(wǎng)絡(luò)請求:
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 獲取數(shù)據(jù) */ 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 Map<String, String> headers; public HttpUrlSource(String url) { this(url, ProxyCacheUtils.getSupposablyMime(url)); } public HttpUrlSource(String url, Map<String, String> 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) { //設(shè)置請求頭 for (Map.Entry<String, String> 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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- OkHttp攔截器在Android網(wǎng)絡(luò)中的使用和工作原理
- Android入門之使用OKHttp多線程下載文件
- Android 使用 okhttp3和retrofit2 進(jìn)行單文件和多文件上傳
- Android基于OkHttp實(shí)現(xiàn)文件上傳功能
- Android使用OKhttp3實(shí)現(xiàn)登錄注冊功能+springboot搭建后端的詳細(xì)過程
- Android的簡單前后端交互(okHttp+springboot+mysql)
- Android Okhttp斷點(diǎn)續(xù)傳面試深入解析
- Android使用OkHttp發(fā)送post請求
- Android使用OkHttp進(jìn)行網(wǎng)絡(luò)同步異步操作
- Android OkHttp實(shí)現(xiàn)全局過期token自動(dòng)刷新示例
- OkHttp原理分析小結(jié)
相關(guān)文章
Android自定義view實(shí)現(xiàn)雪花特效實(shí)例代碼
實(shí)現(xiàn)雪花的效果其實(shí)也可以通過自定義View的方式來實(shí)現(xiàn)的,而且操作上也相對簡單一些,下面這篇文章主要給大家介紹了關(guān)于Android自定義view實(shí)現(xiàn)雪花特效的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Android透明化和沉浸式狀態(tài)欄實(shí)踐及源碼分析
這篇文章主要介紹了Android透明化和沉浸式狀態(tài)欄實(shí)踐及源碼分析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Android入門教程之Vibrator(振動(dòng)器)
本節(jié)我們介紹的是Vibrator(振動(dòng)器),是手機(jī)自帶的振動(dòng)器,其實(shí)就是Android給我們提供的用于機(jī)身震動(dòng)的一個(gè)服務(wù)!當(dāng)收到推送消息的時(shí)候我們可以設(shè)置震動(dòng)提醒。2016-07-07Android簡單實(shí)現(xiàn) 緩存數(shù)據(jù)
這篇文章主要介紹了Android簡單實(shí)現(xiàn) 緩存數(shù)據(jù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Android開發(fā)實(shí)現(xiàn)NFC刷卡讀取的兩種方式
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)中實(shí)現(xiàn)NFC刷卡讀取的兩種方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09ListView的View回收引起的checkbox狀態(tài)改變監(jiān)聽等問題解決方案
之前講到了自定義Adapter傳遞給ListView時(shí),因?yàn)長istView的View回收,需要注意當(dāng)ListView列表項(xiàng)中包含有帶有狀態(tài)標(biāo)識控件的問題,感興趣的朋友可以祥看本文,或許會(huì)有意外的收獲哦2013-01-01android和服務(wù)器的URLEncodedUtils亂碼編碼問題的解決方案
今天小編就為大家分享一篇關(guān)于android和服務(wù)器的URLEncodedUtils亂碼編碼問題的解決方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03關(guān)于gradle你應(yīng)該知道的一些小事
這篇文章主要給大家介紹了關(guān)于gradle你應(yīng)該知道的一些小事,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用gradle具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標(biāo)簽用法示例
這篇文章主要介紹了Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標(biāo)簽用法,實(shí)例分析了Android中TextView控件的使用技巧,需要的朋友可以參考下2016-07-07