欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android視頻點播的實現(xiàn)代碼(邊播邊緩存)

 更新時間:2017年05月09日 10:48:36   作者:顧修忠  
本篇文章主要結(jié)合了Android視頻點播的實現(xiàn)代碼(邊播邊緩存),具有一定的參考價值,感興趣的小伙伴們可以參考一下

簡述

一些知名的視頻app客戶端(優(yōu)酷,愛奇藝)播放視頻的時候都有一些緩存進度(二級進度緩存),還有一些短視頻app,都有邊播邊緩的處理。還有就是當(dāng)文件緩存完畢了再次播放的話就不再請求網(wǎng)絡(luò)了直接播放本地文件了。既節(jié)省了流程又提高了加載速度。
今天我們就是來研究討論實現(xiàn)這個邊播邊緩存的框架,因為它不和任何的業(yè)務(wù)邏輯耦合。

開源的項目

目前比較好的開源項目是:https://github.com/danikula/AndroidVideoCache

代碼的架構(gòu)寫的也很不錯,網(wǎng)絡(luò)用的httpurlconnect,文件緩存處理,文件最大限度策略,回調(diào)監(jiān)聽處理,斷點續(xù)傳,代理服務(wù)等。很值得研究閱讀.

個人覺得項目中有幾個需要優(yōu)化的點,今天就來處理這幾個并簡要分析下原理

優(yōu)化點比如:

  1. 文件的緩存超過限制后沒有按照lru算法刪除,
  2. 處理返回給播放器的http響應(yīng)頭消息,響應(yīng)頭消息的獲取處理改為head請求(需服務(wù)器支持)
  3. 替換網(wǎng)絡(luò)庫為okhttp(因為大部分的項目都是以okhttp為網(wǎng)絡(luò)請求庫的)

該開源項目的原理分析-本地代理


  1. 采用了本地代理服務(wù)的方式,通過原始url給播放器返回一個本地代理的一個url ,代理URL類似:http://127.0.0.1:57430/xxxx;然后播放器播放的時候請求到了你本地的代理上了。
  2. 本地代理采用ServerSocket監(jiān)聽127.0.0.1的有效端口,這個時候手機就是一個服務(wù)器了,客戶端就是socket,也就是播放器。
  3. 讀取客戶端就是socket來讀取數(shù)據(jù)(http協(xié)議請求)解析http協(xié)議。
  4. 根據(jù)url檢查視頻文件是否存在,讀取文件數(shù)據(jù)給播放器,也就是往socket里寫入數(shù)據(jù)。同時如果沒有下載完成會進行斷點下載,當(dāng)然弱網(wǎng)的話數(shù)據(jù)需要生產(chǎn)消費同步處理。

優(yōu)化點

1. 文件的緩存超過限制后沒有按照lru算法刪除.

Files類。

由于在移動設(shè)備上file.setLastModified() 方法不支持毫秒級的時間處理,導(dǎo)致超出限制大小后本應(yīng)該刪除老的,卻沒有刪除拋出了異常。注釋掉主動拋出的異常即可。因為文件的修改時間就是對的。

  static void setLastModifiedNow(File file) throws IOException {
    if (file.exists()) {
      long now = System.currentTimeMillis();
      boolean modified = file.setLastModified(now/1000*1000); // on some devices (e.g. Nexus 5) doesn't work
      if (!modified) {
        modify(file);
//        if (file.lastModified() < now) {
//          VideoCacheLog.debug("LruDiskUsage", "modified not ok ");
//          throw new IOException("Error set last modified date to " + file);
//        }else{
//          VideoCacheLog.debug("LruDiskUsage", "modified ok ");
//        }
      }
    }
  }

2. 處理返回給播放器的http響應(yīng)頭消息,響應(yīng)頭消息的獲取處理改為head請求(需要服務(wù)器支持)

HttpUrlSource類。fetchContentInfo方法是獲取視頻文件的Content-Type,Content-Length信息,是為了播放器播放的時候給播放器組裝http響應(yīng)頭信息用的。所以這一塊需要用數(shù)據(jù)庫保存,這樣播放器每次播放的時候不要在此獲取了,減少了請求的次數(shù),節(jié)省了流量。既然是只需要頭信息,不需要響應(yīng)體,所以我們在獲取的時候可以直接采用HEAD方法。所以代碼增加了一個方法openConnectionForHeader如下:

 private void fetchContentInfo() throws ProxyCacheException {
    VideoCacheLog.debug(TAG,"Read content info from " + sourceInfo.url);
    HttpURLConnection urlConnection = null;
    InputStream inputStream = null;
    try {
      urlConnection = openConnectionForHeader(20000);
      long length = getContentLength(urlConnection);
      String mime = urlConnection.getContentType();
      inputStream = urlConnection.getInputStream();
      this.sourceInfo = new SourceInfo(sourceInfo.url, length, mime);
      this.sourceInfoStorage.put(sourceInfo.url, sourceInfo);
      VideoCacheLog.debug(TAG,"Source info fetched: " + sourceInfo);
    } catch (IOException e) {
      VideoCacheLog.error(TAG,"Error fetching info from " + sourceInfo.url ,e);
    } finally {
      ProxyCacheUtils.close(inputStream);
      if (urlConnection != null) {
        urlConnection.disconnect();
      }
    }
  }

  // for HEAD
  private HttpURLConnection openConnectionForHeader(int timeout) throws IOException, ProxyCacheException {
    HttpURLConnection connection;
    boolean redirected;
    int redirectCount = 0;
    String url = this.sourceInfo.url;
    do {
      VideoCacheLog.debug(TAG, "Open connection for header to " + url);
      connection = (HttpURLConnection) new URL(url).openConnection();
      if (timeout > 0) {
        connection.setConnectTimeout(timeout);
        connection.setReadTimeout(timeout);
      }
      //只返回頭部,不需要BODY,既可以提高響應(yīng)速度也可以減少網(wǎng)絡(luò)流量
      connection.setRequestMethod("HEAD");
      int code = connection.getResponseCode();
      redirected = code == HTTP_MOVED_PERM || code == HTTP_MOVED_TEMP || code == HTTP_SEE_OTHER;
      if (redirected) {
        url = connection.getHeaderField("Location");
        VideoCacheLog.debug(TAG,"Redirect to:" + url);
        redirectCount++;
        connection.disconnect();
        VideoCacheLog.debug(TAG,"Redirect closed:" + url);
      }
      if (redirectCount > MAX_REDIRECTS) {
        throw new ProxyCacheException("Too many redirects: " + redirectCount);
      }
    } while (redirected);
    return connection;
  }

3.替換網(wǎng)絡(luò)庫為okhttp(因為大部分的項目都是以okhttp為網(wǎng)絡(luò)請求庫的)

為什么我們要換呢?!一是OKHttp是一款高效的HTTP客戶端,支持連接同一地址的鏈接共享同一個socket,通過連接池來減小響應(yīng)延遲,還有透明的GZIP壓縮,請求緩存等優(yōu)勢,其核心主要有路由、連接協(xié)議、攔截器、代理、安全性認(rèn)證、連接池以及網(wǎng)絡(luò)適配,攔截器主要是指添加,移除或者轉(zhuǎn)換請求或者回應(yīng)的頭部信息。得到了android開發(fā)的認(rèn)可。二是大部分的app都是采用OKHttp,而且google會將其納入android 源碼中。三是該作者代碼中用的httpurlconnet在HttpUrlSource有這么一段:

 @Override
  public void close() throws ProxyCacheException {
    if (connection != null) {
      try {
        connection.disconnect();
      } catch (NullPointerException | IllegalArgumentException e) {
        String message = "Wait... but why? WTF!? " +
            "Really shouldn't happen any more after fixing https://github.com/danikula/AndroidVideoCache/issues/43. " +
            "If you read it on your device log, please, notify me danikula@gmail.com or create issue here " +
            "https://github.com/danikula/AndroidVideoCache/issues.";
        throw new RuntimeException(message, e);
      } catch (ArrayIndexOutOfBoundsException e) {
        VideoCacheLog.error(TAG,"Error closing connection correctly. Should happen only on Android L. " +
            "If anybody know how to fix it, please visit https://github.com/danikula/AndroidVideoCache/issues/88. " +
            "Until good solution is not know, just ignore this issue :(", e);
      }
    }
  }

在沒有像okhttp這些優(yōu)秀的網(wǎng)絡(luò)開源項目之前,android開發(fā)都是采用httpurlconnet或者h(yuǎn)ttpclient,部分手機可能會遇到這個問題哈。

這里采用的 compile 'com.squareup.okhttp:okhttp:2.7.5' 版本的來實現(xiàn)該類的功能。在原作者的架構(gòu)思路上我們只需要增加實現(xiàn)Source接口的類OkHttpUrlSource即可,可見作者的代碼架構(gòu)還是不錯的,當(dāng)然我們同樣需要處理上文中提高的優(yōu)化點2中的問題。將項目中所有用到HttpUrlSource的地方改為OkHttpUrlSource即可。
源碼如下:

/**
 * ================================================
 * 作  者:顧修忠

 * 版  本:
 * 創(chuàng)建日期:2017/4/13-上午12:03
 * 描  述:在一些Android手機上HttpURLConnection.disconnect()方法仍然耗時太久,
 * 進行導(dǎo)致MediaPlayer要等待很久才會開始播放,因此決定使用okhttp替換HttpURLConnection
 */

public class OkHttpUrlSource implements Source {

  private static final String TAG = OkHttpUrlSource.class.getSimpleName();
  private static final int MAX_REDIRECTS = 5;
  private final SourceInfoStorage sourceInfoStorage;
  private SourceInfo sourceInfo;
  private OkHttpClient okHttpClient = new OkHttpClient();
  private Call requestCall = null;
  private InputStream inputStream;

  public OkHttpUrlSource(String url) {
    this(url, SourceInfoStorageFactory.newEmptySourceInfoStorage());
  }

  public OkHttpUrlSource(String url, SourceInfoStorage sourceInfoStorage) {
    this.sourceInfoStorage = checkNotNull(sourceInfoStorage);
    SourceInfo sourceInfo = sourceInfoStorage.get(url);
    this.sourceInfo = sourceInfo != null ? sourceInfo :
        new SourceInfo(url, Integer.MIN_VALUE, ProxyCacheUtils.getSupposablyMime(url));
  }

  public OkHttpUrlSource(OkHttpUrlSource source) {
    this.sourceInfo = source.sourceInfo;
    this.sourceInfoStorage = source.sourceInfoStorage;
  }

  @Override
  public synchronized long length() throws ProxyCacheException {
    if (sourceInfo.length == Integer.MIN_VALUE) {
      fetchContentInfo();
    }
    return sourceInfo.length;
  }

  @Override
  public void open(long offset) throws ProxyCacheException {
    try {
      Response response = openConnection(offset, -1);
      String mime = response.header("Content-Type");
      this.inputStream = new BufferedInputStream(response.body().byteStream(), DEFAULT_BUFFER_SIZE);
      long length = readSourceAvailableBytes(response, offset, response.code());
      this.sourceInfo = new SourceInfo(sourceInfo.url, length, mime);
      this.sourceInfoStorage.put(sourceInfo.url, sourceInfo);
    } catch (IOException e) {
      throw new ProxyCacheException("Error opening okHttpClient for " + sourceInfo.url + " with offset " + offset, e);
    }
  }

  private long readSourceAvailableBytes(Response response, long offset, int responseCode) throws IOException {
    long contentLength = getContentLength(response);
    return responseCode == HTTP_OK ? contentLength
        : responseCode == HTTP_PARTIAL ? contentLength + offset : sourceInfo.length;
  }

  private long getContentLength(Response response) {
    String contentLengthValue = response.header("Content-Length");
    return contentLengthValue == null ? -1 : Long.parseLong(contentLengthValue);
  }

  @Override
  public void close() throws ProxyCacheException {
    if (okHttpClient != null && inputStream != null && requestCall != null) {
      try {
        inputStream.close();
        requestCall.cancel();
      } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage(), e);
      }
    }
  }

  @Override
  public int read(byte[] buffer) throws ProxyCacheException {
    if (inputStream == null) {
      throw new ProxyCacheException("Error reading data from " + sourceInfo.url + ": okHttpClient is absent!");
    }
    try {
      return inputStream.read(buffer, 0, buffer.length);
    } catch (InterruptedIOException e) {
      throw new InterruptedProxyCacheException("Reading source " + sourceInfo.url + " is interrupted", e);
    } catch (IOException e) {
      throw new ProxyCacheException("Error reading data from " + sourceInfo.url, e);
    }
  }

  private void fetchContentInfo() throws ProxyCacheException {
    VideoCacheLog.debug(TAG, "Read content info from " + sourceInfo.url);
    Response response = null;
    InputStream inputStream = null;
    try {
      response = openConnectionForHeader(20000);
      if (response == null || !response.isSuccessful()) {
        throw new ProxyCacheException("Fail to fetchContentInfo: " + sourceInfo.url);
      }
      long length = getContentLength(response);
      String mime = response.header("Content-Type", "application/mp4");
      inputStream = response.body().byteStream();
      this.sourceInfo = new SourceInfo(sourceInfo.url, length, mime);
      this.sourceInfoStorage.put(sourceInfo.url, sourceInfo);
      VideoCacheLog.info(TAG, "Content info for `" + sourceInfo.url + "`: mime: " + mime + ", content-length: " + length);
    } catch (IOException e) {
      VideoCacheLog.error(TAG, "Error fetching info from " + sourceInfo.url, e);
    } finally {
      ProxyCacheUtils.close(inputStream);
      if (response != null && requestCall != null) {
        requestCall.cancel();
      }
    }
  }

  // for HEAD
  private Response openConnectionForHeader(int timeout) throws IOException, ProxyCacheException {
    if (timeout > 0) {
//      okHttpClient.setConnectTimeout(timeout, TimeUnit.MILLISECONDS);
//      okHttpClient.setReadTimeout(timeout, TimeUnit.MILLISECONDS);
//      okHttpClient.setWriteTimeout(timeout, TimeUnit.MILLISECONDS);
    }
    Response response;
    boolean isRedirect = false;
    String newUrl = this.sourceInfo.url;
    int redirectCount = 0;
    do {
      //只返回頭部,不需要BODY,既可以提高響應(yīng)速度也可以減少網(wǎng)絡(luò)流量
      Request request = new Request.Builder()
          .head()
          .url(newUrl)
          .build();
      requestCall = okHttpClient.newCall(request);
      response = requestCall.execute();
      if (response.isRedirect()) {
        newUrl = response.header("Location");
        VideoCacheLog.debug(TAG, "Redirect to:" + newUrl);
        isRedirect = response.isRedirect();
        redirectCount++;
        requestCall.cancel();
        VideoCacheLog.debug(TAG, "Redirect closed:" + newUrl);
      }
      if (redirectCount > MAX_REDIRECTS) {
        throw new ProxyCacheException("Too many redirects: " + redirectCount);
      }
    } while (isRedirect);

    return response;
  }

  private Response openConnection(long offset, int timeout) throws IOException, ProxyCacheException {
    if (timeout > 0) {
//      okHttpClient.setConnectTimeout(timeout, TimeUnit.MILLISECONDS);
//      okHttpClient.setReadTimeout(timeout, TimeUnit.MILLISECONDS);
//      okHttpClient.setWriteTimeout(timeout, TimeUnit.MILLISECONDS);
    }
    Response response;
    boolean isRedirect = false;
    String newUrl = this.sourceInfo.url;
    int redirectCount = 0;
    do {
      VideoCacheLog.debug(TAG, "Open connection" + (offset > 0 ? " with offset " + offset : "") + " to " + sourceInfo.url);
      Request.Builder requestBuilder = new Request.Builder()
          .get()
          .url(newUrl);
      if (offset > 0) {
        requestBuilder.addHeader("Range", "bytes=" + offset + "-");
      }
      requestCall = okHttpClient.newCall(requestBuilder.build());
      response = requestCall.execute();
      if (response.isRedirect()) {
        newUrl = response.header("Location");
        isRedirect = response.isRedirect();
        redirectCount++;
      }
      if (redirectCount > MAX_REDIRECTS) {
        throw new ProxyCacheException("Too many redirects: " + redirectCount);
      }
    } while (isRedirect);

    return response;
  }

  public synchronized String getMime() throws ProxyCacheException {
    if (TextUtils.isEmpty(sourceInfo.mime)) {
      fetchContentInfo();
    }
    return sourceInfo.mime;
  }

  public String getUrl() {
    return sourceInfo.url;
  }

  @Override
  public String toString() {
    return "OkHttpUrlSource{sourceInfo='" + sourceInfo + "}";
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android入門之Activity間互相傳值詳解

    Android入門之Activity間互相傳值詳解

    我們在之前的Service篇章中看到了一種putExtras和getExtras來進行activity與service間的傳值。而恰恰這種傳值其實也是Android里的通用傳值法。它同樣可以適用在activity與activity間傳值,本文就來和大家詳細(xì)講講
    2022-12-12
  • Android使用WebView播放flash的方法

    Android使用WebView播放flash的方法

    這篇文章主要介紹了Android使用WebView播放flash及判斷是否安裝flash插件的方法,以實例形式詳細(xì)講述了從布局、邏輯判斷到功能最終實現(xiàn)播放Flash的方法,是Android程序設(shè)計中比較典型的應(yīng)用,需要的朋友可以參考下
    2014-11-11
  • Android簡單修改原有應(yīng)用和添加應(yīng)用的方法

    Android簡單修改原有應(yīng)用和添加應(yīng)用的方法

    這篇文章主要介紹了Android簡單修改原有應(yīng)用和添加應(yīng)用的方法,涉及Android工程項目中針對源碼的修改及資源文件的編譯等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-10-10
  • Android平臺基于Pull方式對XML文件解析與寫入方法詳解

    Android平臺基于Pull方式對XML文件解析與寫入方法詳解

    這篇文章主要介紹了Android平臺基于Pull方式對XML文件解析與寫入方法,結(jié)合實例形式分析了Android基于Pull方式對xml文件解析及寫入操作的步驟、原理與操作技巧,需要的朋友可以參考下
    2016-10-10
  • Android Activity啟動模式之standard實例詳解

    Android Activity啟動模式之standard實例詳解

    這篇文章主要介紹了Android Activity啟動模式之standard,結(jié)合實例形式較為詳細(xì)的分析了Android中活動(Activity)四種啟動模式中的standard相關(guān)注意事項與實現(xiàn)技巧,需要的朋友可以參考下
    2016-01-01
  • Android賬號注冊實現(xiàn)點擊獲取驗證碼倒計時效果

    Android賬號注冊實現(xiàn)點擊獲取驗證碼倒計時效果

    這篇文章主要為大家詳細(xì)介紹了Android賬號注冊過程中實現(xiàn)點擊獲取驗證碼倒計時效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android中ImageView的使用方法

    Android中ImageView的使用方法

    這篇文章主要為大家詳細(xì)介紹了Android中ImageView的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android開發(fā)之機頂盒上gridview和ScrollView的使用詳解

    Android開發(fā)之機頂盒上gridview和ScrollView的使用詳解

    這篇文章主要介紹了Android開發(fā)之機頂盒上gridview和ScrollView的使用詳解的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • Bitmap海量數(shù)據(jù)快速查找去重代碼示例

    Bitmap海量數(shù)據(jù)快速查找去重代碼示例

    這篇文章主要介紹了Bitmap海量數(shù)據(jù)快速查找去重代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-12-12
  • Android開發(fā)之Adobe flash操作工具類

    Android開發(fā)之Adobe flash操作工具類

    這篇文章主要介紹了Android開發(fā)之Adobe flash操作工具類,可實現(xiàn)flash的安裝及判斷flash是否安裝等功能,需要的朋友可以參考下
    2017-12-12

最新評論