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

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

編輯:Android開發實例

本文將會教大家如何實現一個簡單的代理服務器(僅支持Http Get),與Android的MediaPlayer結合,從而可以擴展出“播放 防盜鏈的媒體文件”,“邊播放邊保存”等的功能。

       本文的代碼可以到這裡下載:http://download.csdn.net/detail/hellogv/4047134,代碼分為兩個工程:

  1. J2SE工程:HttpGetProxy,在PC上實現簡單的代理服務器,核心類是HttpGetProxy.java,非常容易使用,這裡就不唠叨了直接貼出運行效果圖:
  2. Android工程:本文重點,必須唠叨一下。MediaPlayer播放網絡音頻與HttpGetProxy.java結合,通過代理服務器播放網絡音頻。

   接下來貼出HttpGetProxy.java的原理圖:

 接下來貼出HttpGetProxy.java的源碼:

通過RemoteSocket的out_remoteSocket可以訪問防盜鏈資源,HttpGetProxy通過2個線程來實現轉發,可以在兩個線程內實現保存的功能。

 

  1. package com.musicplayer; 
  2.  
  3. import java.io.IOException; 
  4. import java.io.InputStream; 
  5. import java.io.OutputStream; 
  6. import java.net.InetAddress; 
  7. import java.net.InetSocketAddress; 
  8. import java.net.ServerSocket; 
  9. import java.net.Socket; 
  10. import java.net.SocketAddress; 
  11. import java.net.UnknownHostException; 
  12.  
  13. import android.util.Log; 
  14.  
  15. public class HttpGetProxy { 
  16.     final private String LOCAL_IP_ADDRESS = "127.0.0.1"; 
  17.     final private int HTTP_PORT = 80; 
  18.  
  19.     private ServerSocket localServer = null; 
  20.     private Socket localSocket = null; 
  21.     private Socket remoteSocket = null; 
  22.     private String remoteIPAddress; 
  23.  
  24.     private InputStream in_remoteSocket; 
  25.     private OutputStream out_remoteSocket; 
  26.     private InputStream in_localSocket; 
  27.     private OutputStream out_localSocket; 
  28.      
  29.     private interface OnFinishListener { 
  30.         void onFinishListener(); 
  31.     } 
  32.      
  33.     public HttpGetProxy(int localport) { 
  34.  
  35.         // --------建立代理中轉服務器-----------// 
  36.         try { 
  37.             localServer = new ServerSocket(localport, 1, 
  38.                     InetAddress.getByName(LOCAL_IP_ADDRESS)); 
  39.         } catch (UnknownHostException e) { 
  40.             // TODO Auto-generated catch block 
  41.             e.printStackTrace(); 
  42.         } catch (IOException e) { 
  43.             // TODO Auto-generated catch block 
  44.             e.printStackTrace(); 
  45.         } 
  46.     } 
  47.  
  48.     /** 
  49.      * 結束時,清除所有資源 
  50.      */ 
  51.     private OnFinishListener finishListener =new OnFinishListener(){ 
  52.  
  53.         @Override 
  54.         public void onFinishListener() { 
  55.             System.out.println("..........release all.........."); 
  56.             Log.e("---->","..........release all.........."); 
  57.             try { 
  58.                 in_localSocket.close(); 
  59.                 out_remoteSocket.close(); 
  60.                  
  61.                 in_remoteSocket.close(); 
  62.                 out_localSocket.close(); 
  63.                  
  64.                 localSocket.close(); 
  65.                 remoteSocket.close(); 
  66.             } catch (IOException e) { 
  67.                 // TODO Auto-generated catch block 
  68.                 e.printStackTrace(); 
  69.             } 
  70.         } 
  71.     }; 
  72.      
  73.     public void startProxy(String remoteIpAddr) throws IOException { 
  74.         remoteIPAddress = remoteIpAddr; 
  75.         SocketAddress address = new InetSocketAddress(remoteIPAddress,HTTP_PORT); 
  76.  
  77.         // --------連接目標服務器---------// 
  78.         remoteSocket = new Socket(); 
  79.         remoteSocket.connect(address); 
  80.         System.out.println("..........remote Server connected.........."); 
  81.         Log.e("---->","..........remote Server connected.........."); 
  82.         in_remoteSocket = remoteSocket.getInputStream(); 
  83.         out_remoteSocket = remoteSocket.getOutputStream(); 
  84.         System.out.println("..........init remote Server I/O.........."); 
  85.         /** 
  86.          * 接收本地request,並轉發到遠程服務器 
  87.          */ 
  88.         new Thread() { 
  89.             public void run() { 
  90.                 int bytes_read; 
  91.                 byte[] local_request = new byte[5120]; 
  92.                 try { 
  93.                     // 本地Socket 
  94.                     localSocket = localServer.accept(); 
  95.                     System.out.println("..........localSocket connected.........."); 
  96.                     Log.e("---->","..........localSocket connected.........."); 
  97.                     in_localSocket = localSocket.getInputStream(); 
  98.                     out_localSocket = localSocket.getOutputStream(); 
  99.  
  100.                     System.out.println("..........init local Socket I/O.........."); 
  101.                     Log.e("---->","..........init local Socket I/O.........."); 
  102.                     String buffer = ""; 
  103.                     while ((bytes_read = in_localSocket.read(local_request)) != -1) { 
  104.                         String str = new String(local_request); 
  105.                         System.out.println("localSocket     " + str); 
  106.                         Log.e("localSocket---->",str); 
  107.                         buffer = buffer + str; 
  108.                         if (buffer.contains("GET") 
  109.                                 && buffer.contains("\r\n\r\n")) { 
  110.                             //---把request中的本地ip改為遠程ip---// 
  111.                             buffer = buffer.replace(LOCAL_IP_ADDRESS,remoteIPAddress); 
  112.                             System.out.println("已經替換IP"); 
  113.                             out_remoteSocket.write(buffer.getBytes()); 
  114.                             out_remoteSocket.flush(); 
  115.                             continue; 
  116.                         } else{ 
  117.                             out_remoteSocket.write(buffer.getBytes()); 
  118.                             out_remoteSocket.flush(); 
  119.                         } 
  120.                     } 
  121.                     System.out.println("..........local finish receive..........."); 
  122.                     Log.e("---->","..........local finish receive.........."); 
  123.                     finishListener.onFinishListener(); 
  124.                 } catch (IOException e) { 
  125.                     // TODO Auto-generated catch block 
  126.                     e.printStackTrace(); 
  127.                 } 
  128.             } 
  129.         }.start(); 
  130.  
  131.         /** 
  132.          * 接收遠程服務器reply,並轉發到本地客戶端 
  133.          */ 
  134.         new Thread() { 
  135.             public void run() { 
  136.                 int bytes_read; 
  137.                 byte[] remote_reply = new byte[5120]; 
  138.                 try { 
  139.                     System.out.println("..........remote start to receive..........."); 
  140.                     Log.e("---->","..........remote start to receive.........."); 
  141.                     while ((bytes_read = in_remoteSocket.read(remote_reply)) != -1) { 
  142.                          
  143.                         //System.out.println("remoteSocket     " + remote_reply.length); 
  144.                         //System.out.println("remoteSocket     " + new String(remote_reply)); 
  145.                         out_localSocket.write(remote_reply, 0, bytes_read); 
  146.                         out_localSocket.flush(); 
  147.                     } 
  148.                     System.out.println("..........remote finish receive..........."); 
  149.                     Log.e("---->","..........remote finish receive.........."); 
  150.                 } catch (IOException e) { 
  151.                     // TODO Auto-generated catch block 
  152.                     e.printStackTrace(); 
  153.                 } 
  154.             } 
  155.         }.start(); 
  156.     } 

 

 

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