Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

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:31.775: E/from Media Server---->(449): HTTP/1.1 200 OK
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): Range: bytes=720896-
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): 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:51.461: E/HttpGetProxy(449): ..........sckPlayer connected..........MediaPlayer發出請求
07-29 15:51:51.471: E/to Media Server---->(449): Range: bytes=196608-
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): Content-Range: bytes 196608-754776/754777

07-29 15:51:54.812: E/HttpGetProxy(449): ..........sckPlayer connected..........MediaPlayer發出請求
07-29 15:51:54.822: E/to Media Server---->(449): Range: bytes=589824-
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): Content-Range: bytes 589824-754776/754777

從這些 Log 資訊不難看出:雖然只手動 seek 了一次,但 MediaPlayer 卻發出了多次 Range 請求。這說明了「MediaPlayer 會自動 seek」,或許與其本地緩存機制有關。此外,不同硬解廠商實現的 MediaPlayer,估計也會有不同的操作行為。

本文的程式碼可在此下載:https://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.  
  18. /**收發Media Player請求的Socket*/
  19. private Socket sckPlayer = null;
  20. /**收發Media Server請求的Socket*/
  21. private Socket sckServer = null;
  22.  
  23. private SocketAddress address;
  24.  
  25. /**
  26. * 初始化代理服務器
  27. * @param localport 代理服務器監聽的端口
  28. */
  29. public HttpGetProxy(int localport) {
  30. try {
  31.  
  32. _HttpGetProxy(LOCAL_IP_ADDRESS_1,localport);
  33. } catch (Exception e) {
  34.  
  35. Log.e(TAG,LOCAL_IP_ADDRESS_1+"???"+e.toString());
  36. try {
  37. _HttpGetProxy(LOCAL_IP_ADDRESS_2,localport);
  38. }catch (Exception e1) {
  39. Log.e(TAG,LOCAL_IP_ADDRESS_2+"???"+e.toString());
  40. System.exit(0);
  41. }
  42. }
  43.  
  44. }
  45.  
  46. private void _HttpGetProxy(String ipAddress,int localport) throws UnknownHostException, IOException {
  47. proxy_ip_port=localport;
  48.  
  49. localServer = new ServerSocket(localport,1,InetAddress.getByName(ipAddress));
  50.  
  51. localHost=ipAddress;
  52.  
  53. }
  54.  
  55. /**
  56. * 把網絡URL轉為本地URL,127.0.0.1替換網絡域名
  57. * @param url 網絡URL
  58. * @return 本地URL
  59. */
  60. public String getLocalURL(String urlString){
  61.  
  62. //----排除HTTP特殊----//
  63. String targetUrl=ProxyUtils.getRedirectUrl(urlString);
  64.  
  65. //----獲取對應本地代理服務器的鏈接----//
  66. String result = null;
  67.  
  68. URI originalURI=URI.create(targetUrl);
  69. remoteHost=originalURI.getHost();
  70.  
  71. if(originalURI.getPort()!=-1){//URL帶Port
  72. address = new InetSocketAddress(remoteHost,originalURI.getPort());//使用默認端口
  73.  
  74. original_ip_port = originalURI.getPort()+"";//保存端口,中轉時替換
  75.  
  76. result=targetUrl.replace(remoteHost+":"+originalURI.getPort(),
  77.  
  78. localHost+":"+proxy_ip_port);
  79.  
  80. }
  81.  
  82. else{//URL不帶Port
  83.  
  84. address = new InetSocketAddress(remoteHost,HTTP_PORT);//使用80端口
  85.  
  86. original_ip_port = "";
  87.  
  88. result=targetUrl.replace(remoteHost,localHost+":"+proxy_ip_port);
  89.  
  90. }
  91.  
  92. return result;
  93.  
  94. }
  95.  
  96.  
  97. /**
  98. * 啟動代理服務器
  99. * @throws IOException
  100. */
  101.  
  102. public void asynStartProxy(){
  103.  
  104. new Thread() {
  105.  
  106. public void run() {
  107.  
  108. int bytes_read;
  109.  
  110. byte[] local_request = new byte[1024];
  111.  
  112. byte[] remote_reply = new byte[1024];
  113.  
  114. while (true) {
  115.  
  116. try {
  117.  
  118. //--------------------------------------
  119.  
  120. //監聽MediaPlayer的請求,MediaPlayer->代理服務器
  121.  
  122. //--------------------------------------
  123.  
  124. sckPlayer = localServer.accept();
  125.  
  126. Log.e(TAG, "..........sckPlayer connected..........");
  127.  
  128. String requestStr = "";
  129.  
  130. while ((bytes_read = sckPlayer.getInputStream().read(local_request)) != -1) {
  131.  
  132. byte[] tmpBuffer=new byte[bytes_read];
  133.  
  134. System.arraycopy(local_request,0,tmpBuffer,0,bytes_read);
  135.  
  136. String str = new String(tmpBuffer);
  137.  
  138. requestStr = requestStr + str;
  139.  
  140. if (requestStr.contains("GET"
  141.  
  142. && requestStr.contains(HTTP_END)) {
  143.  
  144. break;
  145. }
  146.  
  147. }
  148.  
  149. //把request中的本地ip改為遠程ip
  150.  
  151. requestStr = requestStr.replace(localHost,remoteHost);
  152.  
  153. //把代理服務器端口改為原URL端口
  154.  
  155. if(TextUtils.isEmpty(original_ip_port))
  156.  
  157. requestStr = requestStr.replace(":"+proxy_ip_port, "");
  158.  
  159. else
  160.  
  161. requestStr = requestStr.replace(":"+proxy_ip_port, ":"+original_ip_port);
  162.  
  163.  
  164. Log.e("to Media Server---->", requestStr);
  165.  
  166. //--------------------------------------
  167.  
  168. //把MediaPlayer的請求發到網絡服務器
  169.  
  170. //--------------------------------------
  171.  
  172. sckServer = new Socket();
  173.  
  174. sckServer.connect(address);
  175.  
  176. Log.e(TAG,"..........remote Server connected..........");
  177.  
  178. sckServer.getOutputStream().write(requestStr.getBytes());
  179.  
  180. //------------------------------------------------------
  181.  
  182. //把網絡服務器的反饋發到MediaPlayer
  183.  
  184. //------------------------------------------------------
  185.  
  186. Log.e(TAG,"..........remote start to receive..........");
  187.  
  188. String responseStr = "";
  189.  
  190. boolean isCaptured=false;
  191.  
  192. while ((bytes_read = sckServer.getInputStream().read(remote_reply)) != -1) {
  193.  
  194. byte[] tmpBuffer=new byte[bytes_read];
  195.  
  196. System.arraycopy(remote_reply,0,tmpBuffer,0,bytes_read);
  197.  
  198. //----捕獲收到的Response文本內容----//
  199.  
  200. if (!isCaptured) {
  201.  
  202. String str = new String(tmpBuffer);
  203.  
  204. responseStr += str;
  205.  
  206. if (responseStr.contains("HTTP/"
  207.  
  208. && responseStr.contains(HTTP_END)) {
  209.  
  210. int endIndex=responseStr.indexOf(HTTP_END, 0);
  211.  
  212. responseStr=responseStr.substring(0, endIndex);
  213.  
  214. Log.e("from Media Server---->", responseStr);
  215.  
  216. isCaptured=true;
  217.  
  218. }
  219.  
  220. }
  221.  
  222. sckPlayer.getOutputStream().write(tmpBuffer);
  223.  
  224. sckPlayer.getOutputStream().flush();
  225.  
  226. }
  227.  
  228. Log.e(TAG, "..........over..........");
  229.  
  230. //關閉對內,對內 2個SOCKET
  231.  
  232. sckPlayer.close();
  233.  
  234. sckServer.close();
  235.  
  236. } catch (IOException e) {
  237.  
  238. e.printStackTrace();
  239.  
  240. }
  241.  
  242. }
  243.  
  244. }
  245.  
  246. }.start();
  247.  
  248. }
  249.  
  250. }

從程式碼與 Log 可以歸納出代理伺服器的核心職責:它監聽 MediaPlayer 的連線,擷取並解析 HTTP 請求,將其中的本地位址與端口替換回遠程伺服器的位址後轉發出去;接著接收來源伺服器的回應,一邊把 Response 的文字標頭擷取下來記錄,一邊將資料原封不動地回寫給 MediaPlayer,完成整段播放的握手與資料透傳。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved