本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!
本文是在《玩转 Android MediaPlayer之视频预加载(优化)》基础上修复Http代理服务器(Http Proxy)透传的bug。前面几篇相关文章所用的代理服务器一个时间只能监听来自Mediaplayer的一个Request请求,但在实际项目开发过程中,发现有些支持m3u8格式Mediaplayer发出新的Request请求之前不会中断旧的Request请求,所以本文代码会加入多线程监听Request请求。
本文代码可以在这里下载:http://download.csdn.net/detail/hellogv/4894109
代理服务器被初始化之后,开始2个线程监听MediaPlayer的Request请求:
public HttpGetProxy(String dirPath,int size,int maximum) { try { //初始化代理服务器 mBufferDirPath = dirPath; mBufferSize=size; mBufferFileMaximum = maximum; localHost = Config.LOCAL_IP_ADDRESS; localServer = new ServerSocket(0, 1,InetAddress.getByName(localHost)); localPort =localServer.getLocalPort();//有ServerSocket自动分配端口 //启动代理服务器 new Thread() { public void run() { startProxy(); } }.start(); mEnable = true; } catch (Exception e) { mEnable = false; } }
private void startProxy() { while (true) { // -------------------------------------- // 监听MediaPlayer的请求,MediaPlayer->代理服务器 // -------------------------------------- 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)); } } }代理服务器收到Request请求(seek的操作)之后,用新线程建立Socket,而旧的Socket会因为OutputStream的write异常而进入异常处理,所以要在异常处理里回收Socket资源。本文的代理服务器在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
- 6楼yangc_83前天 19:32
- 顶个
- 5楼Pinkpinkpanther3天前 14:23
- 没得沙发了哦
- 4楼xuyan871013天前 14:04
- 顶虫哥,亚日居然不在!
- 3楼Iptton5天前 11:14
- 顶专家
- 2楼eyu88745215天前 09:54
- 哇哦,好久没发博客了,赶紧占领沙发
- 1楼qxe8885天前 14:48
- 棒极了,赞