Android MediaPlayer与Http Proxy结合之优化篇

本文修复了Android MediaPlayer使用Http代理服务器时的Bug,解决了多Request请求并发问题,通过多线程监听实现请求处理,适用于m3u8格式播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

               

本文来自http://blog.youkuaiyun.com/hellogv/ ,引用必须注明出处!

    本文是在《玩转 Android MediaPlayer之视频预加载(优化)》基础上修复Http代理服务器(Http Proxy)透传的bug。前面几篇相关文章所用的代理服务器一个时间只能监听来自Mediaplayer的一个Request请求,但在实际项目开发过程中,发现有些支持m3u8格式Mediaplayer发出新的Request请求之前不会中断旧的Request请求,所以本文代码会加入多线程监听Request请求。

    本文代码可以在这里下载:http://download.youkuaiyun.com/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


           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值