Android MediaPlayer與Http Proxy結合之優化篇

本文是在《玩轉 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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章