Android MediaPlayer與Http Proxy結合之最佳化篇

來源:互聯網
上載者:User

本文來自http://blog.csdn.net/hellogv/ ,引用必須註明出處!

  本文是在《玩轉 Android MediaPlayer之視頻預先載入(最佳化)》基礎上修複HttpProxy 伺服器(Http Proxy)透傳的bug。前面幾篇相關文章所用的Proxy 伺服器一個時間只能監聽來自Mediaplayer的一個Request請求,但在實際項目開發過程中,發現有些支援m3u8格式Mediaplayer發出新的Request請求之前不會中斷舊的Request請求,所以本文代碼會加入多線程監聽Request請求。

  本文代碼可以在這裡下載:http://download.csdn.net/detail/hellogv/4894109

  Proxy 伺服器被初始化之後,開始2個線程監聽MediaPlayer的Request請求:

public HttpGetProxy(String dirPath,int size,int maximum) {try {//初始化Proxy 伺服器mBufferDirPath = dirPath; mBufferSize=size;mBufferFileMaximum = maximum;localHost = Config.LOCAL_IP_ADDRESS;localServer = new ServerSocket(0, 1,InetAddress.getByName(localHost));localPort =localServer.getLocalPort();//有ServerSocket自動分配連接埠//啟動Proxy 伺服器new Thread() {public void run() {startProxy();}}.start();mEnable = true;} catch (Exception e) {mEnable = false;}}


private void startProxy() {while (true) {// --------------------------------------// 監聽MediaPlayer的請求,MediaPlayer->Proxy 伺服器// --------------------------------------Log.i(TAG, "......ready to start...........");try {Socket s = localServer.accept();if(proxy!=null){proxy.closeSockets();}Log.i(TAG, "......started...........");proxy = new Proxy(s);new Thread(){public void run(){Log.i(TAG, "......ready to start...........");try {Socket s = localServer.accept();proxy.closeSockets();Log.i(TAG, "......started...........");proxy = new Proxy(s);proxy.run();} catch (IOException e) {Log.e(TAG, e.toString());Log.e(TAG, Utils.getExceptionMessage(e));}}}.start();proxy.run();} catch (IOException e) {Log.e(TAG, e.toString());Log.e(TAG, Utils.getExceptionMessage(e));}}}

  Proxy 伺服器收到Request請求(seek的操作)之後,用新線程建立Socket,而舊的Socket會因為OutputStream的write異常而進入異常處理,所以要在異常處理裡回收Socket資源。本文的Proxy 伺服器在mediaplayer seek之後的log如下:

12-16 13:55:53.181: I/HttpParser<---(27624): HTTP/1.1 206 Partial Content12-16 13:55:53.181: I/HttpParser<---(27624): Content-Length: 66513973312-16 13:55:53.181: I/HttpParser<---(27624): Content-Type: application/force-download12-16 13:55:53.181: I/HttpParser<---(27624): Content-Range: bytes 2906976-668046708/66804670912-16 13:55:53.181: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT12-16 13:55:53.181: I/HttpParser<---(27624): Accept-Ranges: bytes12-16 13:55:53.181: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"12-16 13:55:53.181: I/HttpParser<---(27624): Server: Microsoft-IIS/6.012-16 13:55:53.181: I/HttpParser<---(27624): Content-Disposition: attachment12-16 13:55:53.181: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:55:45 GMT12-16 13:55:53.181: I/HttpParser<---(27624): Connection: close12-16 13:55:53.181: I/HttpParser<---(27624): 12-16 13:55:59.631: I/HttpGetProxy(27624): ......started...........12-16 13:55:59.631: I/HttpGetProxy(27624): <----------------------------------->12-16 13:55:59.641: I/HttpParser(27624): GET /%E6%89%8B%E6%9C%BA%E7%94%B5%E5%BD%B1/201212/%E5%BF%97%E6%98%8E%E4%B8%8E%E6%98%A5%E5%A8%87-2mp4-800x448.mp4 HTTP/1.112-16 13:55:59.641: I/HttpParser(27624): User-Agent: AppleCoreMedia/1.0.0.8C148 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh_cn)12-16 13:55:59.641: I/HttpParser(27624): Accept: */*12-16 13:55:59.641: I/HttpParser(27624): Range: bytes=401793617-12-16 13:55:59.641: I/HttpParser(27624): Connection: close12-16 13:55:59.641: I/HttpParser(27624): Host: d1.2mp4.net12-16 13:55:59.641: I/HttpParser(27624): 12-16 13:55:59.641: I/HttpParser(27624): ------->rangePosition:40179361712-16 13:55:59.641: E/HttpGetProxy(27624): java.net.SocketException: Socket is closed12-16 13:55:59.641: E/HttpGetProxy(27624): java.net.Socket.checkOpenAndCreate  641line12-16 13:55:59.641: E/HttpGetProxy(27624): java.net.Socket.getOutputStream  381line12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxyUtils.sendToMP  112line12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$Proxy.run  292line12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.startProxy  213line12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.access$8  183line12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$1.run  78line12-16 13:55:59.641: I/HttpGetProxy(27624): ......ready to start...........12-16 13:56:01.181: I/HttpParser<---(27624): HTTP/1.1 206 Partial Content12-16 13:56:01.181: I/HttpParser<---(27624): Content-Length: 26625309212-16 13:56:01.181: I/HttpParser<---(27624): Content-Type: application/force-download12-16 13:56:01.181: I/HttpParser<---(27624): Content-Range: bytes 401793617-668046708/66804670912-16 13:56:01.181: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT12-16 13:56:01.181: I/HttpParser<---(27624): Accept-Ranges: bytes12-16 13:56:01.181: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"12-16 13:56:01.181: I/HttpParser<---(27624): Server: Microsoft-IIS/6.012-16 13:56:01.181: I/HttpParser<---(27624): Content-Disposition: attachment12-16 13:56:01.181: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:55:54 GMT12-16 13:56:01.181: I/HttpParser<---(27624): Connection: close12-16 13:56:01.181: I/HttpParser<---(27624): 12-16 13:56:01.181: I/HttpGetProxy(27624): ----------------->需要發送預先載入到MediaPlayer12-16 13:56:01.181: I/HttpGetProxy(27624): >>>不讀取預先載入檔案 range:401793617,buffer:290697612-16 13:56:13.761: E/HttpGetProxy(27624): java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)12-16 13:56:13.761: E/HttpGetProxy(27624): libcore.io.IoBridge.maybeThrowAfterSendto  496line12-16 13:56:13.761: E/HttpGetProxy(27624): libcore.io.IoBridge.sendto  465line12-16 13:56:13.761: E/HttpGetProxy(27624): java.net.PlainSocketImpl.write  507line12-16 13:56:13.761: E/HttpGetProxy(27624): java.net.PlainSocketImpl.access$100  46line12-16 13:56:13.761: E/HttpGetProxy(27624): java.net.PlainSocketImpl$PlainSocketOutputStream.write  269line12-16 13:56:13.761: E/HttpGetProxy(27624): com.proxy.HttpGetProxyUtils.sendToMP  112line12-16 13:56:13.761: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$Proxy.run  292line12-16 13:56:13.761: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$2.run  205line12-16 13:56:13.771: I/HttpGetProxy(27624): ......started...........12-16 13:56:13.771: I/HttpGetProxy(27624): <----------------------------------->12-16 13:56:13.771: I/HttpParser(27624): GET /%E6%89%8B%E6%9C%BA%E7%94%B5%E5%BD%B1/201212/%E5%BF%97%E6%98%8E%E4%B8%8E%E6%98%A5%E5%A8%87-2mp4-800x448.mp4 HTTP/1.112-16 13:56:13.771: I/HttpParser(27624): User-Agent: AppleCoreMedia/1.0.0.8C148 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh_cn)12-16 13:56:13.771: I/HttpParser(27624): Accept: */*12-16 13:56:13.771: I/HttpParser(27624): Range: bytes=612718942-12-16 13:56:13.771: I/HttpParser(27624): Connection: close12-16 13:56:13.771: I/HttpParser(27624): Host: d1.2mp4.net12-16 13:56:13.771: I/HttpParser(27624): 12-16 13:56:13.771: I/HttpParser(27624): ------->rangePosition:61271894212-16 13:56:13.781: I/HttpGetProxy(27624): ......ready to start...........12-16 13:56:14.051: I/HttpParser<---(27624): HTTP/1.1 206 Partial Content12-16 13:56:14.051: I/HttpParser<---(27624): Content-Length: 5532776712-16 13:56:14.051: I/HttpParser<---(27624): Content-Type: application/force-download12-16 13:56:14.051: I/HttpParser<---(27624): Content-Range: bytes 612718942-668046708/66804670912-16 13:56:14.051: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT12-16 13:56:14.051: I/HttpParser<---(27624): Accept-Ranges: bytes12-16 13:56:14.051: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"12-16 13:56:14.051: I/HttpParser<---(27624): Server: Microsoft-IIS/6.012-16 13:56:14.051: I/HttpParser<---(27624): Content-Disposition: attachment12-16 13:56:14.051: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:56:06 GMT12-16 13:56:14.051: I/HttpParser<---(27624): Connection: close12-16 13:56:14.051: I/HttpParser<---(27624): 12-16 13:56:14.051: I/HttpGetProxy(27624): ----------------->需要發送預先載入到MediaPlayer12-16 13:56:14.051: I/HttpGetProxy(27624): >>>不讀取預先載入檔案 range:612718942,buffer:290697612-16 13:56:49.051: I/HttpGetProxy(27624): ......started...........12-16 13:56:49.051: I/HttpGetProxy(27624): <----------------------------------->12-16 13:56:49.051: E/HttpGetProxy(27624): java.net.SocketException: Socket closed12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.Posix.recvfromBytes  -2line12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.Posix.recvfrom  131line12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.BlockGuardOs.recvfrom  164line12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.IoBridge.recvfrom  503line12-16 13:56:49.051: E/HttpGetProxy(27624): java.net.PlainSocketImpl.read  488line12-16 13:56:49.051: E/HttpGetProxy(27624): java.net.PlainSocketImpl.access$000  46line12-16 13:56:49.051: E/HttpGetProxy(27624): java.net.PlainSocketImpl$PlainSocketInputStream.read  240line12-16 13:56:49.051: E/HttpGetProxy(27624): java.io.InputStream.read  163line12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$Proxy.run  288line12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.startProxy  213line12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.access$8  183line12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$1.run  78line12-16 13:56:49.051: I/HttpGetProxy(27624): ......ready to start...........12-16 13:56:49.051: I/HttpParser(27624): GET /%E6%89%8B%E6%9C%BA%E7%94%B5%E5%BD%B1/201212/%E5%BF%97%E6%98%8E%E4%B8%8E%E6%98%A5%E5%A8%87-2mp4-800x448.mp4 HTTP/1.112-16 13:56:49.051: I/HttpParser(27624): User-Agent: AppleCoreMedia/1.0.0.8C148 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh_cn)12-16 13:56:49.051: I/HttpParser(27624): Accept: */*12-16 13:56:49.051: I/HttpParser(27624): Range: bytes=152226030-12-16 13:56:49.051: I/HttpParser(27624): Connection: close12-16 13:56:49.051: I/HttpParser(27624): Host: d1.2mp4.net12-16 13:56:49.051: I/HttpParser(27624): 12-16 13:56:49.051: I/HttpParser(27624): ------->rangePosition:15222603012-16 13:56:49.311: I/HttpParser<---(27624): HTTP/1.1 206 Partial Content12-16 13:56:49.311: I/HttpParser<---(27624): Content-Length: 51582067912-16 13:56:49.311: I/HttpParser<---(27624): Content-Type: application/force-download12-16 13:56:49.311: I/HttpParser<---(27624): Content-Range: bytes 152226030-668046708/66804670912-16 13:56:49.311: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT12-16 13:56:49.311: I/HttpParser<---(27624): Accept-Ranges: bytes12-16 13:56:49.311: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"12-16 13:56:49.311: I/HttpParser<---(27624): Server: Microsoft-IIS/6.012-16 13:56:49.311: I/HttpParser<---(27624): Content-Disposition: attachment12-16 13:56:49.311: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:56:42 GMT12-16 13:56:49.311: I/HttpParser<---(27624): Connection: close12-16 13:56:49.311: I/HttpParser<---(27624): 12-16 13:56:49.311: I/HttpGetProxy(27624): ----------------->需要發送預先載入到MediaPlayer12-16 13:56:49.311: I/HttpGetProxy(27624): >>>不讀取預先載入檔案 range:152226030,buffer:2906976

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.