Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android MediaPlayer之Media Proxy使用開發實例

Android MediaPlayer之Media Proxy使用開發實例

編輯:Android開發實例

      本文是在 Android實現MediaPlayer與Http Proxy結合使用示例(二)  基礎上,進一步優化代理服務器,支持了Http的302、301重定向,獲取Http Request和Http Response的文本內容。本文以視頻播放結合Http Proxy,講述MediaPlayer播放過程中的握手過程。

吐槽一下:用google搜索“代理服務器”無效,所以本文用Media Proxy,大家懂的......

先來看看本文程序運行的截屏動畫:

 

 

 

 

 

再來看看程序運行時輸出的Log信息.....這裡是關鍵:

07-29 15:51:30.692: E/HttpGetProxy(449): ..........sckPlayer connected.......... MediaPlayer發出請求
07-29 15:51:30.692: E/to Media Server---->(449): GET /vids/family_guy_penis_car.3gp HTTP/1.1

07-29 15:51:30.692: E/to Media Server---->(449): User-Agent: stagefright/1.0 (Linux;Android 2.2)

07-29 15:51:30.692: E/to Media Server---->(449): Host: daily3gp.com

07-29 15:51:30.692: E/to Media Server---->(449):

07-29 15:51:31.119: E/HttpGetProxy(449): ..........remote Server connected..........
07-29 15:51:31.122: E/HttpGetProxy(449): ..........remote start to receive..........
07-29 15:51:31.775: E/from Media Server---->(449): HTTP/1.1 200 OK

07-29 15:51:31.775: E/from Media Server---->(449): Date: Sun, 29 Jul 2012 15:51:33 GMT

07-29 15:51:31.775: E/from Media Server---->(449): Server: Apache

07-29 15:51:31.775: E/from Media Server---->(449): Last-Modified: Thu, 14 Jan 2010 23:29:02 GMT

07-29 15:51:31.775: E/from Media Server---->(449): Accept-Ranges: bytes

07-29 15:51:31.775: E/from Media Server---->(449): Content-Length: 754777

07-29 15:51:31.775: E/from Media Server---->(449): Content-Type: video/3gpp


07-29 15:51:34.512: E/HttpGetProxy(449): ..........sckPlayer connected..........MediaPlayer發出請求
07-29 15:51:34.532: E/to Media Server---->(449): GET /vids/family_guy_penis_car.3gp HTTP/1.1

07-29 15:51:34.532: E/to Media Server---->(449): User-Agent: stagefright/1.0 (Linux;Android 2.2)

07-29 15:51:34.532: E/to Media Server---->(449): Host: daily3gp.com

07-29 15:51:34.532: E/to Media Server---->(449): Range: bytes=720896-

07-29 15:51:34.532: E/to Media Server---->(449):

07-29 15:51:34.873: E/HttpGetProxy(449): ..........remote Server connected..........
07-29 15:51:34.873: E/HttpGetProxy(449): ..........remote start to receive..........
07-29 15:51:35.505: E/from Media Server---->(449): HTTP/1.1 206 Partial Content

07-29 15:51:35.505: E/from Media Server---->(449): Date: Sun, 29 Jul 2012 15:51:36 GMT

07-29 15:51:35.505: E/from Media Server---->(449): Server: Apache

07-29 15:51:35.505: E/from Media Server---->(449): Last-Modified: Thu, 14 Jan 2010 23:29:02 GMT

07-29 15:51:35.505: E/from Media Server---->(449): Accept-Ranges: bytes

07-29 15:51:35.505: E/from Media Server---->(449): Content-Length: 33881

07-29 15:51:35.505: E/from Media Server---->(449): Content-Range: bytes 720896-754776/754777

07-29 15:51:35.505: E/from Media Server---->(449): Content-Type: video/3gpp
07-29 15:51:38.754: E/HttpGetProxy(449): ..........over..........


07-29 15:51:51.461: E/HttpGetProxy(449): ..........sckPlayer connected..........MediaPlayer發出請求
07-29 15:51:51.471: E/to Media Server---->(449): GET /vids/family_guy_penis_car.3gp HTTP/1.1

07-29 15:51:51.471: E/to Media Server---->(449): User-Agent: stagefright/1.0 (Linux;Android 2.2)

07-29 15:51:51.471: E/to Media Server---->(449): Host: daily3gp.com

07-29 15:51:51.471: E/to Media Server---->(449): Range: bytes=196608-

07-29 15:51:51.471: E/to Media Server---->(449):

07-29 15:51:51.722: E/HttpGetProxy(449): ..........remote Server connected..........
07-29 15:51:51.722: E/HttpGetProxy(449): ..........remote start to receive..........
07-29 15:51:52.285: E/from Media Server---->(449): HTTP/1.1 206 Partial Content

07-29 15:51:52.285: E/from Media Server---->(449): Date: Sun, 29 Jul 2012 15:51:53 GMT

07-29 15:51:52.285: E/from Media Server---->(449): Server: Apache

07-29 15:51:52.285: E/from Media Server---->(449): Last-Modified: Thu, 14 Jan 2010 23:29:02 GMT

07-29 15:51:52.285: E/from Media Server---->(449): Accept-Ranges: bytes

07-29 15:51:52.285: E/from Media Server---->(449): Content-Length: 558169

07-29 15:51:52.285: E/from Media Server---->(449): Content-Range: bytes 196608-754776/754777

07-29 15:51:52.285: E/from Media Server---->(449): Content-Type: video/3gpp


07-29 15:51:54.812: E/HttpGetProxy(449): ..........sckPlayer connected..........MediaPlayer發出請求
07-29 15:51:54.822: E/to Media Server---->(449): GET /vids/family_guy_penis_car.3gp HTTP/1.1

07-29 15:51:54.822: E/to Media Server---->(449): User-Agent: stagefright/1.0 (Linux;Android 2.2)

07-29 15:51:54.822: E/to Media Server---->(449): Host: daily3gp.com

07-29 15:51:54.822: E/to Media Server---->(449): Range: bytes=589824-

07-29 15:51:54.822: E/to Media Server---->(449):

07-29 15:51:55.117: E/HttpGetProxy(449): ..........remote Server connected..........
07-29 15:51:55.117: E/HttpGetProxy(449): ..........remote start to receive..........
07-29 15:51:55.693: E/from Media Server---->(449): HTTP/1.1 206 Partial Content

07-29 15:51:55.693: E/from Media Server---->(449): Date: Sun, 29 Jul 2012 15:51:57 GMT

07-29 15:51:55.693: E/from Media Server---->(449): Server: Apache

07-29 15:51:55.693: E/from Media Server---->(449): Last-Modified: Thu, 14 Jan 2010 23:29:02 GMT

07-29 15:51:55.693: E/from Media Server---->(449): Accept-Ranges: bytes

07-29 15:51:55.693: E/from Media Server---->(449): Content-Length: 164953

07-29 15:51:55.693: E/from Media Server---->(449): Content-Range: bytes 589824-754776/754777

07-29 15:51:55.693: E/from Media Server---->(449): Content-Type: video/3gpp
07-29 15:51:59.620: E/HttpGetProxy(449): ..........over..........

 

從截屏動畫和Log信息看出,手動seek一次,但MediaPlayer進行了多次Range請求,這說明了“MediaPlayer會自動seek”,或許與MediaPlayer本地緩存有關。另外,不同硬解廠家實現的MediaPlayer估計會有不同的操作。

本文的代碼可以到這裡下載:

http://download.csdn.net/detail/hellogv/4463651

HttpGetProxy.JAVA還是本文的關鍵部分:

 

  1. public class HttpGetProxy {   
  2.     final static private String TAG = "HttpGetProxy";   
  3.     final static private String LOCAL_IP_ADDRESS_1 = "127.0.0.1"; 
  4.     final static private String LOCAL_IP_ADDRESS_2 = "10.0.2.2"; 
  5.     final static private int HTTP_PORT = 80; 
  6.     final static private String HTTP_END="\r\n\r\n"; 
  7.      
  8.     /**代理服務器使用的端口*/ 
  9.     private int proxy_ip_port; 
  10.     /**鏈接帶的端口*/ 
  11.     private String original_ip_port; 
  12.     /**遠程服務器地址*/ 
  13.     private String remoteHost; 
  14.     /**本地服務器地址*/ 
  15.     private String localHost; 
  16.     private ServerSocket localServer = null;   
  17.     /**收發Media Player請求的Socket*/ 
  18.     private Socket sckPlayer = null; 
  19.     /**收發Media Server請求的Socket*/ 
  20.     private Socket sckServer = null; 
  21.    
  22.     private SocketAddress address;   
  23.     
  24.     /**  
  25.      * 初始化代理服務器  
  26.      * @param localport 代理服務器監聽的端口  
  27.      */   
  28.     public HttpGetProxy(int localport) {   
  29.         try { 
  30.             _HttpGetProxy(LOCAL_IP_ADDRESS_1,localport); 
  31.         } catch (Exception e) { 
  32.             Log.e(TAG,LOCAL_IP_ADDRESS_1+"???"+e.toString()); 
  33.             try { 
  34.                 _HttpGetProxy(LOCAL_IP_ADDRESS_2,localport); 
  35.             }catch (Exception e1) { 
  36.                 Log.e(TAG,LOCAL_IP_ADDRESS_2+"???"+e.toString()); 
  37.                 System.exit(0); 
  38.             } 
  39.         } 
  40.     } 
  41.      
  42.     private void _HttpGetProxy(String ipAddress,int localport) throws UnknownHostException, IOException {   
  43.         proxy_ip_port=localport;   
  44.         localServer = new ServerSocket(localport,1,InetAddress.getByName(ipAddress)); 
  45.         localHost=ipAddress; 
  46.     } 
  47.     
  48.     /**  
  49.      * 把網絡URL轉為本地URL,127.0.0.1替換網絡域名  
  50.      * @param url 網絡URL   
  51.      * @return 本地URL  
  52.      */   
  53.     public String getLocalURL(String urlString){ 
  54.         //----排除HTTP特殊----// 
  55.         String targetUrl=ProxyUtils.getRedirectUrl(urlString); 
  56.         //----獲取對應本地代理服務器的鏈接----// 
  57.         String result = null; 
  58.         URI originalURI=URI.create(targetUrl);   
  59.         remoteHost=originalURI.getHost();   
  60.         if(originalURI.getPort()!=-1){//URL帶Port 
  61.             address = new InetSocketAddress(remoteHost,originalURI.getPort());//使用默認端口   
  62.             original_ip_port = originalURI.getPort()+"";//保存端口,中轉時替換 
  63.             result=targetUrl.replace(remoteHost+":"+originalURI.getPort(),   
  64.                     localHost+":"+proxy_ip_port);   
  65.         }   
  66.         else{//URL不帶Port   
  67.             address = new InetSocketAddress(remoteHost,HTTP_PORT);//使用80端口  
  68.             original_ip_port = ""; 
  69.             result=targetUrl.replace(remoteHost,localHost+":"+proxy_ip_port);   
  70.         } 
  71.         return result;      
  72.     }   
  73.      
  74.     /**  
  75.      * 啟動代理服務器  
  76.      * @throws IOException  
  77.      */   
  78.     public void asynStartProxy(){   
  79.         new Thread() {   
  80.             public void run() { 
  81.                 int bytes_read;   
  82.                 byte[] local_request = new byte[1024];   
  83.                 byte[] remote_reply = new byte[1024];   
  84.                 while (true) {   
  85.                     try {   
  86.                         //--------------------------------------   
  87.                         //監聽MediaPlayer的請求,MediaPlayer->代理服務器   
  88.                         //--------------------------------------   
  89.                         sckPlayer = localServer.accept();   
  90.                         Log.e(TAG, "..........sckPlayer connected..........");    
  91.                          
  92.                         String requestStr = ""; 
  93.                         while ((bytes_read = sckPlayer.getInputStream().read(local_request)) != -1) { 
  94.                             byte[] tmpBuffer=new byte[bytes_read];  
  95.                             System.arraycopy(local_request,0,tmpBuffer,0,bytes_read); 
  96.                             String str = new String(tmpBuffer); 
  97.                             //Log.e("from MediaPlayer---->", str);   
  98.                             requestStr = requestStr + str;   
  99.                             if (requestStr.contains("GET")   
  100.                                     && requestStr.contains(HTTP_END)) {   
  101.                                 break; 
  102.                             }    
  103.                         } 
  104.                          
  105.                         //把request中的本地ip改為遠程ip 
  106.                         requestStr = requestStr.replace(localHost,remoteHost); 
  107.                         //把代理服務器端口改為原URL端口 
  108.                         if(TextUtils.isEmpty(original_ip_port)) 
  109.                             requestStr = requestStr.replace(":"+proxy_ip_port, ""); 
  110.                         else 
  111.                             requestStr = requestStr.replace(":"+proxy_ip_port, ":"+original_ip_port); 
  112.  
  113.                         Log.e("to Media Server---->", requestStr); 
  114.                         //--------------------------------------   
  115.                         //把MediaPlayer的請求發到網絡服務器,代理服務器->網絡服務器   
  116.                         //-------------------------------------- 
  117.                         sckServer = new Socket();   
  118.                         sckServer.connect(address);   
  119.                         Log.e(TAG,"..........remote Server connected..........");   
  120.                         sckServer.getOutputStream().write(requestStr.getBytes());//發送MediaPlayer的請求   
  121.                         //------------------------------------------------------   
  122.                         //把網絡服務器的反饋發到MediaPlayer,網絡服務器->代理服務器->MediaPlayer   
  123.                         //------------------------------------------------------ 
  124.                         Log.e(TAG,"..........remote start to receive.........."); 
  125.                         String responseStr = ""; 
  126.                         boolean isCaptured=false; 
  127.                         while ((bytes_read = sckServer.getInputStream().read(remote_reply)) != -1) { 
  128.                             byte[] tmpBuffer=new   byte[bytes_read];  
  129.                             System.arraycopy(remote_reply,0,tmpBuffer,0,bytes_read); 
  130.                             //----捕獲收到的Response文本內容----// 
  131.                             if (!isCaptured) { 
  132.                                 String str = new String(tmpBuffer); 
  133.                                 responseStr += str; 
  134.                                 if (responseStr.contains("HTTP/")   
  135.                                         && responseStr.contains(HTTP_END)) { 
  136.                                      
  137.                                     int endIndex=responseStr.indexOf(HTTP_END, 0); 
  138.                                     responseStr=responseStr.substring(0, endIndex); 
  139.                                     Log.e("from Media Server---->", responseStr); 
  140.                                     isCaptured=true; 
  141.                                 } 
  142.                             } 
  143.                             sckPlayer.getOutputStream().write(tmpBuffer);   
  144.                             sckPlayer.getOutputStream().flush(); 
  145.                         } 
  146.                         Log.e(TAG, "..........over..........");  
  147.                         
  148.                         //關閉對內,對內 2個SOCKET 
  149.                         sckPlayer.close();   
  150.                         sckServer.close();   
  151.                     } catch (IOException e) {   
  152.                         // TODO Auto-generated catch block   
  153.                         e.printStackTrace();   
  154.                     }   
  155.                 }   
  156.             }   
  157.         }.start();   
  158.     }   
  159. }   

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved