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

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

編輯:Android 開發實例

上一篇我們實現了一個簡單的代理伺服器並與 Android 的 MediaPlayer 結合(僅支援 Http Get),可以透過代理伺服器轉發 MediaPlayer 的 Request 以及傳輸伺服器的 Response,但還不能支援 Seek。本篇進階版則成功加入了对 Seek 的支援。

代理伺服器能夠增強 MediaPlayer 對複雜 HTTP 情境的適應能力,例如播放帶有防盜鏈的媒體檔案、邊播邊存,還能對大體積的媒體檔案(如視頻)進行多執行緒預加載,達到快速播放的效果。

本文程式碼運行於模擬器上,並使用 Microsoft Network Monitor 3.4 進行抓包。透過抓包可以發現:Seek 操作會重新連線伺服器,並在 Http Get 請求中帶入 Range 欄位。因此,代理伺服器每次監聽到 MediaPlayer 的 Request 時,都需要新建一個 Socket 與遠程伺服器連線。

本文程式碼可在此下載:https://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. e.printStackTrace();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. /**
  39. * 結束時,清除所有資源
  40. */
  41. private OnFinishListener finishListener = new OnFinishListener() {
  42.  
  43. @Override
  44. public void onFinishListener() {
  45. System.out.println("..........release all..........");
  46. Log.e(TAG, "..........release all..........");
  47. try {
  48. in_localSocket.close();
  49. out_remoteSocket.close();
  50.  
  51. in_remoteSocket.close();
  52. out_localSocket.close();
  53.  
  54. localSocket.close();
  55. remoteSocket.close();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. };
  61.  
  62.  
  63. /**
  64. * 把網絡URL轉為本地URL,127.0.0.1替換網絡域名
  65. * @param url 網絡URL
  66. * @return 本地URL
  67. */
  68. public String getLocalURL(String url){
  69. String result = null;
  70. URI originalURI=URI.create(url);
  71. remoteHost=originalURI.getHost();
  72. if(originalURI.getPort()!=-1){//URL帶Port
  73. address = new InetSocketAddress(remoteHost,
  74. originalURI.getPort());//使用默認端口
  75. result=url.replace(remoteHost+":"+originalURI.getPort(),
  76. LOCAL_IP_ADDRESS+":"+local_ip_port);
  77. }
  78. else{//URL不帶Port
  79. address = new InetSocketAddress(remoteHost,
  80. HTTP_PORT);//使用80端口
  81. result=url.replace(remoteHost,LOCAL_IP_ADDRESS+":"+local_ip_port);
  82. }
  83. return result;
  84.  
  85. }
  86.  
  87. /**
  88. * 啟動代理服務器
  89. * @throws IOException
  90. */
  91. public void startProxy() throws IOException {
  92.  
  93. new Thread() {
  94. public void run() {
  95. int bytes_read;
  96.  
  97. byte[] local_request = new byte[1024];
  98.  
  99. byte[] remote_reply = new byte[1024];
  100.  
  101. while (true) {
  102.  
  103. try {
  104.  
  105. //--------------------------------------
  106.  
  107. //監聽MediaPlayer的請求,MediaPlayer->代理服務器
  108.  
  109. //--------------------------------------
  110.  
  111. localSocket = localServer.accept();
  112.  
  113.  
  114. Log.e(TAG, "..........localSocket connected..........");
  115.  
  116. in_localSocket = localSocket.getInputStream();
  117.  
  118. out_localSocket = localSocket.getOutputStream();
  119.  
  120. Log.e(TAG, "..........init local Socket I/O..........");
  121.  
  122.  
  123. String buffer = "";//保存MediaPlayer的HTTP請求
  124.  
  125. while ((bytes_read = in_localSocket.read(local_request)) != -1) {
  126.  
  127.  
  128. String str = new String(local_request);
  129.  
  130. Log.e("localSocket---->", str);
  131.  
  132. buffer = buffer + str;
  133.  
  134. if (buffer.contains("GET"
  135.  
  136. && buffer.contains("\r\n\r\n")) {
  137.  
  138.  
  139. // ---把request中的本地ip改為遠程ip---//
  140.  
  141. buffer = buffer.replace(LOCAL_IP_ADDRESS,remoteHost);
  142.  
  143. break;
  144.  
  145. }
  146.  
  147. }
  148.  
  149. Log.e(TAG, "..........local finish receive..........");
  150.  
  151.  
  152. //--------------------------------------
  153.  
  154. //把MediaPlayer的請求發到網絡服務器,代理服務器->網絡服務器
  155.  
  156. //--------------------------------------
  157.  
  158. remoteSocket = new Socket();
  159.  
  160. remoteSocket.connect(address);
  161.  
  162. Log.e(TAG,"..........remote Server connected..........");
  163.  
  164. in_remoteSocket = remoteSocket.getInputStream();
  165.  
  166. out_remoteSocket = remoteSocket.getOutputStream();
  167.  
  168. out_remoteSocket.write(buffer.getBytes());//發送MediaPlayer的請求
  169.  
  170. out_remoteSocket.flush();
  171.  
  172.  
  173. //------------------------------------------------------
  174.  
  175. //把網絡服務器的反饋發到MediaPlayer,網絡服務器->代理服務器->MediaPlayer
  176.  
  177. //------------------------------------------------------
  178.  
  179. Log.e(TAG,"..........remote start to receive..........");
  180.  
  181. while ((bytes_read = in_remoteSocket.read(remote_reply)) != -1) {
  182.  
  183.  
  184. out_localSocket.write(remote_reply, 0, bytes_read);
  185.  
  186. out_localSocket.flush();
  187.  
  188. }
  189.  
  190. Log.e(TAG, "..........over..........");
  191.  
  192.  
  193. finishListener.onFinishListener();//釋放資源
  194.  
  195.  
  196. } catch (IOException e) {
  197.  
  198. e.printStackTrace();
  199.  
  200. }
  201.  
  202.  
  203. }
  204.  
  205. }
  206.  
  207. }.start();
  208.  
  209. }
  210. }

總結來說,這個版本的代理伺服器在每次收到 MediaPlayer 的 Request 後,都會新建一個 Socket 與遠程伺服器建立連線,再把請求轉發出去,並將回應原封不動地回寫給播放器。正是因為具備了「每請求一連線」的能力,它才能正確處理 Seek 觸發的多次 Range 請求,為後續的預加載與多執行緒優化奠定了基礎。

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