国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android視頻/音頻緩存框架AndroidVideoCache(Okhttp)詳解

2019-12-12 00:45:21
字體:
來源:轉載
供稿:網友

關于安卓邊下邊播功能,供大家參考,具體內容如下

對于視頻/音頻軟件,音樂軟件,視頻軟件,都有緩存這個功能,那如何實現邊下邊播功能:

  • 如何實現這個邊下邊播功能?
  • 文件是否支持同時讀寫?(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 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) {        //設置請求頭        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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九江县| 东宁县| 罗江县| 博兴县| 潍坊市| 揭阳市| 尖扎县| 河北区| 天祝| 万山特区| 金坛市| 鲜城| 正镶白旗| 宝兴县| 伊宁县| 印江| 会理县| 措美县| 团风县| 湖南省| 连云港市| 手机| 和平县| 绥化市| 沾化县| 卢湾区| 随州市| 霞浦县| 揭阳市| 屏南县| 和林格尔县| 安化县| 龙州县| 宣恩县| 中阳县| 榆中县| 泌阳县| 新竹市| 恩施市| 枣庄市| 东乡族自治县|