Android MediaPlayer与Http Proxy结合之优化篇

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hellogv/article/details/8301504

本文来自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 Content
12-16 13:55:53.181: I/HttpParser<---(27624): Content-Length: 665139733
12-16 13:55:53.181: I/HttpParser<---(27624): Content-Type: application/force-download
12-16 13:55:53.181: I/HttpParser<---(27624): Content-Range: bytes 2906976-668046708/668046709
12-16 13:55:53.181: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT
12-16 13:55:53.181: I/HttpParser<---(27624): Accept-Ranges: bytes
12-16 13:55:53.181: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"
12-16 13:55:53.181: I/HttpParser<---(27624): Server: Microsoft-IIS/6.0
12-16 13:55:53.181: I/HttpParser<---(27624): Content-Disposition: attachment
12-16 13:55:53.181: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:55:45 GMT
12-16 13:55:53.181: I/HttpParser<---(27624): Connection: close
12-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.1
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)
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: close
12-16 13:55:59.641: I/HttpParser(27624): Host: d1.2mp4.net
12-16 13:55:59.641: I/HttpParser(27624): 
12-16 13:55:59.641: I/HttpParser(27624): ------->rangePosition:401793617
12-16 13:55:59.641: E/HttpGetProxy(27624): java.net.SocketException: Socket is closed
12-16 13:55:59.641: E/HttpGetProxy(27624): java.net.Socket.checkOpenAndCreate  641line
12-16 13:55:59.641: E/HttpGetProxy(27624): java.net.Socket.getOutputStream  381line
12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxyUtils.sendToMP  112line
12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$Proxy.run  292line
12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.startProxy  213line
12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.access$8  183line
12-16 13:55:59.641: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$1.run  78line
12-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 Content
12-16 13:56:01.181: I/HttpParser<---(27624): Content-Length: 266253092
12-16 13:56:01.181: I/HttpParser<---(27624): Content-Type: application/force-download
12-16 13:56:01.181: I/HttpParser<---(27624): Content-Range: bytes 401793617-668046708/668046709
12-16 13:56:01.181: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT
12-16 13:56:01.181: I/HttpParser<---(27624): Accept-Ranges: bytes
12-16 13:56:01.181: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"
12-16 13:56:01.181: I/HttpParser<---(27624): Server: Microsoft-IIS/6.0
12-16 13:56:01.181: I/HttpParser<---(27624): Content-Disposition: attachment
12-16 13:56:01.181: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:55:54 GMT
12-16 13:56:01.181: I/HttpParser<---(27624): Connection: close
12-16 13:56:01.181: I/HttpParser<---(27624): 
12-16 13:56:01.181: I/HttpGetProxy(27624): ----------------->需要发送预加载到MediaPlayer
12-16 13:56:01.181: I/HttpGetProxy(27624): >>>不读取预加载文件 range:401793617,buffer:2906976
12-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  496line
12-16 13:56:13.761: E/HttpGetProxy(27624): libcore.io.IoBridge.sendto  465line
12-16 13:56:13.761: E/HttpGetProxy(27624): java.net.PlainSocketImpl.write  507line
12-16 13:56:13.761: E/HttpGetProxy(27624): java.net.PlainSocketImpl.access$100  46line
12-16 13:56:13.761: E/HttpGetProxy(27624): java.net.PlainSocketImpl$PlainSocketOutputStream.write  269line
12-16 13:56:13.761: E/HttpGetProxy(27624): com.proxy.HttpGetProxyUtils.sendToMP  112line
12-16 13:56:13.761: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$Proxy.run  292line
12-16 13:56:13.761: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$2.run  205line
12-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.1
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)
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: close
12-16 13:56:13.771: I/HttpParser(27624): Host: d1.2mp4.net
12-16 13:56:13.771: I/HttpParser(27624): 
12-16 13:56:13.771: I/HttpParser(27624): ------->rangePosition:612718942
12-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 Content
12-16 13:56:14.051: I/HttpParser<---(27624): Content-Length: 55327767
12-16 13:56:14.051: I/HttpParser<---(27624): Content-Type: application/force-download
12-16 13:56:14.051: I/HttpParser<---(27624): Content-Range: bytes 612718942-668046708/668046709
12-16 13:56:14.051: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT
12-16 13:56:14.051: I/HttpParser<---(27624): Accept-Ranges: bytes
12-16 13:56:14.051: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"
12-16 13:56:14.051: I/HttpParser<---(27624): Server: Microsoft-IIS/6.0
12-16 13:56:14.051: I/HttpParser<---(27624): Content-Disposition: attachment
12-16 13:56:14.051: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:56:06 GMT
12-16 13:56:14.051: I/HttpParser<---(27624): Connection: close
12-16 13:56:14.051: I/HttpParser<---(27624): 
12-16 13:56:14.051: I/HttpGetProxy(27624): ----------------->需要发送预加载到MediaPlayer
12-16 13:56:14.051: I/HttpGetProxy(27624): >>>不读取预加载文件 range:612718942,buffer:2906976
12-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 closed
12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.Posix.recvfromBytes  -2line
12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.Posix.recvfrom  131line
12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.BlockGuardOs.recvfrom  164line
12-16 13:56:49.051: E/HttpGetProxy(27624): libcore.io.IoBridge.recvfrom  503line
12-16 13:56:49.051: E/HttpGetProxy(27624): java.net.PlainSocketImpl.read  488line
12-16 13:56:49.051: E/HttpGetProxy(27624): java.net.PlainSocketImpl.access$000  46line
12-16 13:56:49.051: E/HttpGetProxy(27624): java.net.PlainSocketImpl$PlainSocketInputStream.read  240line
12-16 13:56:49.051: E/HttpGetProxy(27624): java.io.InputStream.read  163line
12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$Proxy.run  288line
12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.startProxy  213line
12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy.access$8  183line
12-16 13:56:49.051: E/HttpGetProxy(27624): com.proxy.HttpGetProxy$1.run  78line
12-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.1
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)
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: close
12-16 13:56:49.051: I/HttpParser(27624): Host: d1.2mp4.net
12-16 13:56:49.051: I/HttpParser(27624): 
12-16 13:56:49.051: I/HttpParser(27624): ------->rangePosition:152226030
12-16 13:56:49.311: I/HttpParser<---(27624): HTTP/1.1 206 Partial Content
12-16 13:56:49.311: I/HttpParser<---(27624): Content-Length: 515820679
12-16 13:56:49.311: I/HttpParser<---(27624): Content-Type: application/force-download
12-16 13:56:49.311: I/HttpParser<---(27624): Content-Range: bytes 152226030-668046708/668046709
12-16 13:56:49.311: I/HttpParser<---(27624): Last-Modified: Thu, 13 Dec 2012 14:28:00 GMT
12-16 13:56:49.311: I/HttpParser<---(27624): Accept-Ranges: bytes
12-16 13:56:49.311: I/HttpParser<---(27624): ETag: "80b39bd3ed9cd1:2d6"
12-16 13:56:49.311: I/HttpParser<---(27624): Server: Microsoft-IIS/6.0
12-16 13:56:49.311: I/HttpParser<---(27624): Content-Disposition: attachment
12-16 13:56:49.311: I/HttpParser<---(27624): Date: Sun, 16 Dec 2012 05:56:42 GMT
12-16 13:56:49.311: I/HttpParser<---(27624): Connection: close
12-16 13:56:49.311: I/HttpParser<---(27624): 
12-16 13:56:49.311: I/HttpGetProxy(27624): ----------------->需要发送预加载到MediaPlayer
12-16 13:56:49.311: I/HttpGetProxy(27624): >>>不读取预加载文件 range:152226030,buffer:2906976


阅读更多

没有更多推荐了,返回首页