Android MediaPlayer与Http Proxy结合之优化篇

本文来自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请求:

  1. public HttpGetProxy(String dirPath,int size,int maximum) {  
  2.     try {  
  3.         //初始化代理服务器  
  4.         mBufferDirPath = dirPath;   
  5.         mBufferSize=size;  
  6.         mBufferFileMaximum = maximum;  
  7.         localHost = Config.LOCAL_IP_ADDRESS;  
  8.         localServer = new ServerSocket(01,InetAddress.getByName(localHost));  
  9.         localPort =localServer.getLocalPort();//有ServerSocket自动分配端口  
  10.         //启动代理服务器  
  11.         new Thread() {  
  12.             public void run() {  
  13.                 startProxy();  
  14.             }  
  15.         }.start();  
  16.           
  17.         mEnable = true;  
  18.     } catch (Exception e) {  
  19.         mEnable = false;  
  20.     }  
  21. }  


  1. private void startProxy() {  
  2.     while (true) {  
  3.         // --------------------------------------  
  4.         // 监听MediaPlayer的请求,MediaPlayer->代理服务器  
  5.         // --------------------------------------  
  6.         Log.i(TAG, "......ready to start...........");  
  7.         try {  
  8.             Socket s = localServer.accept();  
  9.             if(proxy!=null){  
  10.                 proxy.closeSockets();  
  11.             }  
  12.             Log.i(TAG, "......started...........");  
  13.             proxy = new Proxy(s);  
  14.               
  15.             new Thread(){  
  16.                 public void run(){  
  17.                     Log.i(TAG, "......ready to start...........");  
  18.                     try {  
  19.                         Socket s = localServer.accept();  
  20.                         proxy.closeSockets();  
  21.                         Log.i(TAG, "......started...........");  
  22.                         proxy = new Proxy(s);  
  23.                         proxy.run();  
  24.                     } catch (IOException e) {  
  25.                         Log.e(TAG, e.toString());  
  26.                         Log.e(TAG, Utils.getExceptionMessage(e));  
  27.                     }  
  28.                       
  29.                 }  
  30.             }.start();  
  31.             proxy.run();  
  32.         } catch (IOException e) {  
  33.             Log.e(TAG, e.toString());  
  34.             Log.e(TAG, Utils.getExceptionMessage(e));  
  35.         }  
  36.     }  
  37. }  
  代理服务器收到Request请求(seek的操作)之后,用新线程建立Socket,而旧的Socket会因为OutputStream的write异常而进入异常处理,所以要在异常处理里回收Socket资源 本文的代理服务器在mediaplayer seek之后的log如下:
  1. 12-16 13:55:53.181: I/HttpParser<---(27624): HTTP/1.1 206 Partial Content  
  2. 12-16 13:55:53.181: I/HttpParser<---(27624): Content-Length: 665139733  
  3. 12-16 13:55:53.181: I/HttpParser<---(27624): Content-Type: application/force-download  
  4. 12-16 13:55:53.181: I/HttpParser<---(27624): Content-Range: bytes 2906976-668046708/668046709  
  5. 12-16 13:55:53.181: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT  
  6. 12-16 13:55:53.181: I/HttpParser<---(27624): Accept-Ranges: bytes  
  7. 12-16 13:55:53.181: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"  
  8. 12-16 13:55:53.181: I/HttpParser<---(27624): Server: Microsoft-IIS/6.0  
  9. 12-16 13:55:53.181: I/HttpParser<---(27624): Content-Disposition: attachment  
  10. 12-16 13:55:53.181: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:55:45 GMT  
  11. 12-16 13:55:53.181: I/HttpParser<---(27624): Connection: close  
  12. 12-16 13:55:53.181: I/HttpParser<---(27624):   
  13. 12-16 13:55:59.631: I/HttpGetProxy(27624): ......started...........  
  14. 12-16 13:55:59.631: I/HttpGetProxy(27624): <----------------------------------->  
  15. 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.1  
  16. 12-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)  
  17. 12-16 13:55:59.641: I/HttpParser(27624): Accept: */*  
  18. 12-16 13:55:59.641: I/HttpParser(27624): Range: bytes=401793617-  
  19. 12-16 13:55:59.641: I/HttpParser(27624): Connection: close  
  20. 12-16 13:55:59.641: I/HttpParser(27624): Host: d1.2mp4.net  
  21. 12-16 13:55:59.641: I/HttpParser(27624):   
  22. 12-16 13:55:59.641: I/HttpParser(27624): ------->rangePosition:401793617  
  23. 12-16 13:55:59.641: E/HttpGetProxy(27624): java.net.SocketException: Socket is closed  
  24. 12-16 13:55:59.641: E/HttpGetProxy(27624): java.net.Socket.checkOpenAndCreate  641line  
  25. 12-16 13:55:59.641: E/HttpGetProxy(27624): java.net.Socket.getOutputStream  381line  
  26. 12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxyUtils.sendToMP  112line  
  27. 12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$Proxy.run  292line  
  28. 12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.startProxy  213line  
  29. 12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.access$8  183line  
  30. 12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$1.run  78line  
  31. 12-16 13:55:59.641: I/HttpGetProxy(27624): ......ready to start...........  
  32. 12-16 13:56:01.181: I/HttpParser<---(27624): HTTP/1.1 206 Partial Content  
  33. 12-16 13:56:01.181: I/HttpParser<---(27624): Content-Length: 266253092  
  34. 12-16 13:56:01.181: I/HttpParser<---(27624): Content-Type: application/force-download  
  35. 12-16 13:56:01.181: I/HttpParser<---(27624): Content-Range: bytes 401793617-668046708/668046709  
  36. 12-16 13:56:01.181: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT  
  37. 12-16 13:56:01.181: I/HttpParser<---(27624): Accept-Ranges: bytes  
  38. 12-16 13:56:01.181: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"  
  39. 12-16 13:56:01.181: I/HttpParser<---(27624): Server: Microsoft-IIS/6.0  
  40. 12-16 13:56:01.181: I/HttpParser<---(27624): Content-Disposition: attachment  
  41. 12-16 13:56:01.181: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:55:54 GMT  
  42. 12-16 13:56:01.181: I/HttpParser<---(27624): Connection: close  
  43. 12-16 13:56:01.181: I/HttpParser<---(27624):   
  44. 12-16 13:56:01.181: I/HttpGetProxy(27624): ----------------->需要发送预加载到MediaPlayer  
  45. 12-16 13:56:01.181: I/HttpGetProxy(27624): >>>不读取预加载文件 range:401793617,buffer:2906976  
  46. 12-16 13:56:13.761: E/HttpGetProxy(27624): java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)  
  47. 12-16 13:56:13.761: E/HttpGetProxy(27624): libcore.io.IoBridge.maybeThrowAfterSendto  496line  
  48. 12-16 13:56:13.761: E/HttpGetProxy(27624): libcore.io.IoBridge.sendto  465line  
  49. 12-16 13:56:13.761: E/HttpGetProxy(27624): java.net.PlainSocketImpl.write  507line  
  50. 12-16 13:56:13.761: E/HttpGetProxy(27624): java.net.PlainSocketImpl.access$100  46line  
  51. 12-16 13:56:13.761: E/HttpGetProxy(27624): java.net.PlainSocketImpl$PlainSocketOutputStream.write  269line  
  52. 12-16 13:56:13.761: E/HttpGetProxy(27624): com.proxy.HttpGetProxyUtils.sendToMP  112line  
  53. 12-16 13:56:13.761: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$Proxy.run  292line  
  54. 12-16 13:56:13.761: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$2.run  205line  
  55. 12-16 13:56:13.771: I/HttpGetProxy(27624): ......started...........  
  56. 12-16 13:56:13.771: I/HttpGetProxy(27624): <----------------------------------->  
  57. 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.1  
  58. 12-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)  
  59. 12-16 13:56:13.771: I/HttpParser(27624): Accept: */*  
  60. 12-16 13:56:13.771: I/HttpParser(27624): Range: bytes=612718942-  
  61. 12-16 13:56:13.771: I/HttpParser(27624): Connection: close  
  62. 12-16 13:56:13.771: I/HttpParser(27624): Host: d1.2mp4.net  
  63. 12-16 13:56:13.771: I/HttpParser(27624):   
  64. 12-16 13:56:13.771: I/HttpParser(27624): ------->rangePosition:612718942  
  65. 12-16 13:56:13.781: I/HttpGetProxy(27624): ......ready to start...........  
  66. 12-16 13:56:14.051: I/HttpParser<---(27624): HTTP/1.1 206 Partial Content  
  67. 12-16 13:56:14.051: I/HttpParser<---(27624): Content-Length: 55327767  
  68. 12-16 13:56:14.051: I/HttpParser<---(27624): Content-Type: application/force-download  
  69. 12-16 13:56:14.051: I/HttpParser<---(27624): Content-Range: bytes 612718942-668046708/668046709  
  70. 12-16 13:56:14.051: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT  
  71. 12-16 13:56:14.051: I/HttpParser<---(27624): Accept-Ranges: bytes  
  72. 12-16 13:56:14.051: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"  
  73. 12-16 13:56:14.051: I/HttpParser<---(27624): Server: Microsoft-IIS/6.0  
  74. 12-16 13:56:14.051: I/HttpParser<---(27624): Content-Disposition: attachment  
  75. 12-16 13:56:14.051: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:56:06 GMT  
  76. 12-16 13:56:14.051: I/HttpParser<---(27624): Connection: close  
  77. 12-16 13:56:14.051: I/HttpParser<---(27624):   
  78. 12-16 13:56:14.051: I/HttpGetProxy(27624): ----------------->需要发送预加载到MediaPlayer  
  79. 12-16 13:56:14.051: I/HttpGetProxy(27624): >>>不读取预加载文件 range:612718942,buffer:2906976  
  80. 12-16 13:56:49.051: I/HttpGetProxy(27624): ......started...........  
  81. 12-16 13:56:49.051: I/HttpGetProxy(27624): <----------------------------------->  
  82. 12-16 13:56:49.051: E/HttpGetProxy(27624): java.net.SocketException: Socket closed  
  83. 12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.Posix.recvfromBytes  -2line  
  84. 12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.Posix.recvfrom  131line  
  85. 12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.BlockGuardOs.recvfrom  164line  
  86. 12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.IoBridge.recvfrom  503line  
  87. 12-16 13:56:49.051: E/HttpGetProxy(27624): java.net.PlainSocketImpl.read  488line  
  88. 12-16 13:56:49.051: E/HttpGetProxy(27624): java.net.PlainSocketImpl.access$000  46line  
  89. 12-16 13:56:49.051: E/HttpGetProxy(27624): java.net.PlainSocketImpl$PlainSocketInputStream.read  240line  
  90. 12-16 13:56:49.051: E/HttpGetProxy(27624): java.io.InputStream.read  163line  
  91. 12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$Proxy.run  288line  
  92. 12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.startProxy  213line  
  93. 12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.access$8  183line  
  94. 12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$1.run  78line  
  95. 12-16 13:56:49.051: I/HttpGetProxy(27624): ......ready to start...........  
  96. 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.1  
  97. 12-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)  
  98. 12-16 13:56:49.051: I/HttpParser(27624): Accept: */*  
  99. 12-16 13:56:49.051: I/HttpParser(27624): Range: bytes=152226030-  
  100. 12-16 13:56:49.051: I/HttpParser(27624): Connection: close  
  101. 12-16 13:56:49.051: I/HttpParser(27624): Host: d1.2mp4.net  
  102. 12-16 13:56:49.051: I/HttpParser(27624):   
  103. 12-16 13:56:49.051: I/HttpParser(27624): ------->rangePosition:152226030  
  104. 12-16 13:56:49.311: I/HttpParser<---(27624): HTTP/1.1 206 Partial Content  
  105. 12-16 13:56:49.311: I/HttpParser<---(27624): Content-Length: 515820679  
  106. 12-16 13:56:49.311: I/HttpParser<---(27624): Content-Type: application/force-download  
  107. 12-16 13:56:49.311: I/HttpParser<---(27624): Content-Range: bytes 152226030-668046708/668046709  
  108. 12-16 13:56:49.311: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT  
  109. 12-16 13:56:49.311: I/HttpParser<---(27624): Accept-Ranges: bytes  
  110. 12-16 13:56:49.311: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"  
  111. 12-16 13:56:49.311: I/HttpParser<---(27624): Server: Microsoft-IIS/6.0  
  112. 12-16 13:56:49.311: I/HttpParser<---(27624): Content-Disposition: attachment  
  113. 12-16 13:56:49.311: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:56:42 GMT  
  114. 12-16 13:56:49.311: I/HttpParser<---(27624): Connection: close  
  115. 12-16 13:56:49.311: I/HttpParser<---(27624):   
  116. 12-16 13:56:49.311: I/HttpGetProxy(27624): ----------------->需要发送预加载到MediaPlayer  
  117. 12-16 13:56:49.311: I/HttpGetProxy(27624): >>>不读取预加载文件 range:152226030,buffer:2906976 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值