• Android MediaPlayer与Http Proxy结合之提高篇


    本文来自http://blog.csdn.net/hellogv/ ,引用.

           基础篇实现一个简单的代理服务器与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.     }  
    156. }  
    157.    
  • 相关阅读:
    寒假周总结一
    1657. Determine if Two Strings Are Close
    1656. Design an Ordered Stream
    695. Max Area of Island (BFS)
    695. Max Area of Island (DFS)
    Daily Coding Problem: Problem #713
    939. Minimum Area Rectangle
    259. 3Sum Smaller
    29. Divide Two Integers
    16. 3Sum Closest
  • 原文地址:https://www.cnblogs.com/new0801/p/6175906.html
Copyright © 2020-2023  润新知