Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android實現MediaPlayer與Http Proxy結合使用示例(二)

Android實現MediaPlayer與Http Proxy結合使用示例(二)

編輯:Android開發實例

        第一篇實現一個簡單的代理服務器與Android的MediaPlayer結合(僅支持Http Get),可以通過代理服務器來轉發MediaPlayer的Request以及傳輸服務器的Response,但是還不能支持Seek,這次提高篇支持了Seek。代理服務器可以增強MediaPlayer對復雜的Http情況的適應,可以播放帶防盜鏈的媒體文件,邊播邊存,還可以對大體積的媒體文件(如視頻)進行多線程預加載,達到快速播放的效果。

       本文代碼運行在模擬器上,使用Microsoft Network Monitor 3.4來抓包,通過抓包可以發現seek的操作會重新連接服務器,並在Http Get請求中加入Range 字段,所以代理服務器每次監聽到MediaPlayer的request都需要新建socket與遠程服務器連接。

本文的代碼可以到http://download.csdn.net/detail/hellogv/4332362下載,本文程序運行效果如圖:

 

 

 

 

 

 

 

 

 

接下來貼出核心代碼HttpGetProxy.java:

  1. public class HttpGetProxy { 
  2.     final static private String TAG = "HttpGetProxy"; 
  3.     final static private String LOCAL_IP_ADDRESS = "127.0.0.1"; 
  4.     final static private int HTTP_PORT = 80; 
  5.      
  6.     private int local_ip_port; 
  7.     private ServerSocket localServer = null; 
  8.     private Socket localSocket = null; 
  9.     private Socket remoteSocket = null; 
  10.     private String remoteHost; 
  11.  
  12.     private InputStream in_remoteSocket; 
  13.     private OutputStream out_remoteSocket; 
  14.     private InputStream in_localSocket; 
  15.     private OutputStream out_localSocket; 
  16.  
  17.     private SocketAddress address; 
  18.     private interface OnFinishListener { 
  19.         void onFinishListener(); 
  20.     } 
  21.  
  22.     /** 
  23.      * 初始化代理服務器 
  24.      * @param localport 代理服務器監聽的端口 
  25.      */ 
  26.     public HttpGetProxy(int localport) { 
  27.         local_ip_port=localport; 
  28.         try { 
  29.             localServer = new ServerSocket(localport, 1, 
  30.                     InetAddress.getByName(LOCAL_IP_ADDRESS)); 
  31.         } catch (UnknownHostException e) { 
  32.             // TODO Auto-generated catch block 
  33.             e.printStackTrace(); 
  34.         } catch (IOException e) { 
  35.             // TODO Auto-generated catch block 
  36.             e.printStackTrace(); 
  37.         } 
  38.     } 
  39.  
  40.     /** 
  41.      * 結束時,清除所有資源 
  42.      */ 
  43.     private OnFinishListener finishListener = new OnFinishListener() { 
  44.  
  45.         @Override 
  46.         public void onFinishListener() { 
  47.             System.out.println("..........release all.........."); 
  48.             Log.e(TAG, "..........release all.........."); 
  49.             try { 
  50.                 in_localSocket.close(); 
  51.                 out_remoteSocket.close(); 
  52.  
  53.                 in_remoteSocket.close(); 
  54.                 out_localSocket.close(); 
  55.  
  56.                 localSocket.close(); 
  57.                 remoteSocket.close(); 
  58.             } catch (IOException e) { 
  59.                 // TODO Auto-generated catch block 
  60.                 e.printStackTrace(); 
  61.             } 
  62.         } 
  63.     }; 
  64.  
  65.  
  66.     /** 
  67.      * 把網絡URL轉為本地URL,127.0.0.1替換網絡域名 
  68.      * @param url 網絡URL 
  69.      * @return 本地URL 
  70.      */ 
  71.     public String getLocalURL(String url){ 
  72.         String result = null; 
  73.         URI originalURI=URI.create(url); 
  74.         remoteHost=originalURI.getHost(); 
  75.         if(originalURI.getPort()!=-1){//URL帶Port 
  76.             address = new InetSocketAddress(remoteHost, 
  77.                     originalURI.getPort());//使用默認端口 
  78.             result=url.replace(remoteHost+":"+originalURI.getPort(), 
  79.                     LOCAL_IP_ADDRESS+":"+local_ip_port); 
  80.         } 
  81.         else{//URL不帶Port 
  82.             address = new InetSocketAddress(remoteHost, 
  83.                     HTTP_PORT);//使用80端口 
  84.             result=url.replace(remoteHost,LOCAL_IP_ADDRESS+":"+local_ip_port); 
  85.         } 
  86.         return result; 
  87.          
  88.     } 
  89.      
  90.     /** 
  91.      * 啟動代理服務器 
  92.      * @throws IOException 
  93.      */ 
  94.     public void startProxy() throws IOException { 
  95.          
  96.         new Thread() { 
  97.             public void run() { 
  98.                 int bytes_read; 
  99.                 byte[] local_request = new byte[1024]; 
  100.                 byte[] remote_reply = new byte[1024]; 
  101.                 while (true) { 
  102.                     try { 
  103.                         //-------------------------------------- 
  104.                         //監聽MediaPlayer的請求,MediaPlayer->代理服務器 
  105.                         //-------------------------------------- 
  106.                         localSocket = localServer.accept(); 
  107.  
  108.                         Log.e(TAG, "..........localSocket connected.........."); 
  109.                         in_localSocket = localSocket.getInputStream(); 
  110.                         out_localSocket = localSocket.getOutputStream(); 
  111.                         Log.e(TAG, "..........init local Socket I/O.........."); 
  112.  
  113.                         String buffer = "";//保存MediaPlayer的HTTP請求 
  114.                         while ((bytes_read = in_localSocket.read(local_request)) != -1) { 
  115.                             String str = new String(local_request); 
  116.                             Log.e("localSocket---->", str); 
  117.                             buffer = buffer + str; 
  118.                             if (buffer.contains("GET") 
  119.                                     && buffer.contains("\r\n\r\n")) { 
  120.                                 // ---把request中的本地ip改為遠程ip---// 
  121.                                 buffer = buffer.replace(LOCAL_IP_ADDRESS,remoteHost); 
  122.                                 break; 
  123.                             } 
  124.                         } 
  125.                         Log.e(TAG, "..........local finish receive.........."); 
  126.  
  127.                         //-------------------------------------- 
  128.                         //把MediaPlayer的請求發到網絡服務器,代理服務器->網絡服務器 
  129.                         //-------------------------------------- 
  130.                         remoteSocket = new Socket(); 
  131.                         remoteSocket.connect(address); 
  132.                         Log.e(TAG,"..........remote Server connected.........."); 
  133.                         in_remoteSocket = remoteSocket.getInputStream(); 
  134.                         out_remoteSocket = remoteSocket.getOutputStream(); 
  135.                         out_remoteSocket.write(buffer.getBytes());//發送MediaPlayer的請求 
  136.                         out_remoteSocket.flush(); 
  137.  
  138.                         //------------------------------------------------------ 
  139.                         //把網絡服務器的反饋發到MediaPlayer,網絡服務器->代理服務器->MediaPlayer 
  140.                         //------------------------------------------------------ 
  141.                         Log.e(TAG,"..........remote start to receive.........."); 
  142.                         while ((bytes_read = in_remoteSocket.read(remote_reply)) != -1) { 
  143.                             out_localSocket.write(remote_reply, 0, bytes_read); 
  144.                             out_localSocket.flush(); 
  145.                         } 
  146.                         Log.e(TAG, "..........over.........."); 
  147.                         finishListener.onFinishListener();//釋放資源 
  148.                     } catch (IOException e) { 
  149.                         // TODO Auto-generated catch block 
  150.                         e.printStackTrace(); 
  151.                     } 
  152.                 } 
  153.             } 
  154.         }.start(); 
  155.     } 

 

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