這是我們為大家提供的一篇介紹如何玩轉 Android MediaPlayer之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:30.692: E/to Media Server---->(449):
07-29 15:51:31.119: E/HttpGetProxy(449): ..........remote Server connected..........
07-29 15:51:31.122: E/HttpGetProxy(449): ..........remote start to receive..........
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): Date: Sun, 29 Jul 2012 15:51:33 GMT
07-29 15:51:31.775: E/from Media Server---->(449): Server: Apache
07-29 15:51:31.775: E/from Media Server---->(449): Last-Modified: Thu, 14 Jan 2010 23:29:02 GMT
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): User-Agent: stagefright/1.0 (Linux;Android 2.2)
07-29 15:51:34.532: E/to Media Server---->(449): Host: daily3gp.com
07-29 15:51:34.532: E/to Media Server---->(449): Range: bytes=720896-
07-29 15:51:34.532: E/to Media Server---->(449):
07-29 15:51:34.873: E/HttpGetProxy(449): ..........remote Server connected..........
07-29 15:51:34.873: E/HttpGetProxy(449): ..........remote start to receive..........
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): Date: Sun, 29 Jul 2012 15:51:36 GMT
07-29 15:51:35.505: E/from Media Server---->(449): Server: Apache
07-29 15:51:35.505: E/from Media Server---->(449): Last-Modified: Thu, 14 Jan 2010 23:29:02 GMT
07-29 15:51:35.505: E/from Media Server---->(449): Accept-Ranges: bytes
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:38.754: E/HttpGetProxy(449): ..........over..........
07-29 15:51:51.461: E/HttpGetProxy(449): ..........sckPlayer connected..........MediaPlayer發出請求
07-29 15:51:51.471: E/to Media Server---->(449): GET /vids/family_guy_penis_car.3gp HTTP/1.1
07-29 15:51:51.471: E/to Media Server---->(449): User-Agent: stagefright/1.0 (Linux;Android 2.2)
07-29 15:51:51.471: E/to Media Server---->(449): Host: daily3gp.com
07-29 15:51:51.471: E/to Media Server---->(449): Range: bytes=196608-
07-29 15:51:51.471: E/to Media Server---->(449):
07-29 15:51:51.722: E/HttpGetProxy(449): ..........remote Server connected..........
07-29 15:51:51.722: E/HttpGetProxy(449): ..........remote start to receive..........
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): Date: Sun, 29 Jul 2012 15:51:53 GMT
07-29 15:51:52.285: E/from Media Server---->(449): Server: Apache
07-29 15:51:52.285: E/from Media Server---->(449): Last-Modified: Thu, 14 Jan 2010 23:29:02 GMT
07-29 15:51:52.285: E/from Media Server---->(449): Accept-Ranges: bytes
07-29 15:51:52.285: E/from Media Server---->(449): Content-Length: 558169
07-29 15:51:52.285: E/from Media Server---->(449): Content-Range: bytes 196608-754776/754777
07-29 15:51:52.285: E/from Media Server---->(449): Content-Type: video/3gpp
07-29 15:51:54.812: E/HttpGetProxy(449): ..........sckPlayer connected..........MediaPlayer發出請求
07-29 15:51:54.822: E/to Media Server---->(449): GET /vids/family_guy_penis_car.3gp HTTP/1.1
07-29 15:51:54.822: E/to Media Server---->(449): User-Agent: stagefright/1.0 (Linux;Android 2.2)
07-29 15:51:54.822: E/to Media Server---->(449): Host: daily3gp.com
07-29 15:51:54.822: E/to Media Server---->(449): Range: bytes=589824-
07-29 15:51:54.822: E/to Media Server---->(449):
07-29 15:51:55.117: E/HttpGetProxy(449): ..........remote Server connected..........
07-29 15:51:55.117: E/HttpGetProxy(449): ..........remote start to receive..........
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): Date: Sun, 29 Jul 2012 15:51:57 GMT
07-29 15:51:55.693: E/from Media Server---->(449): Server: Apache
07-29 15:51:55.693: E/from Media Server---->(449): Last-Modified: Thu, 14 Jan 2010 23:29:02 GMT
07-29 15:51:55.693: E/from Media Server---->(449): Accept-Ranges: bytes
07-29 15:51:55.693: E/from Media Server---->(449): Content-Length: 164953
07-29 15:51:55.693: E/from Media Server---->(449): Content-Range: bytes 589824-754776/754777
07-29 15:51:55.693: E/from Media Server---->(449): Content-Type: video/3gpp
07-29 15:51:59.620: E/HttpGetProxy(449): ..........over..........
從截屏動畫和Log信息看出,手動seek一次,但MediaPlayer進行了多次Range請求,這說明了“MediaPlayer會自動seek”,或許與MediaPlayer本地緩存有關。另外,不同硬解廠家實現的MediaPlayer估計會有不同的操作。
本文的代碼可以到這裡下載:
http://download.csdn.net/detail/hellogv/4463651
HttpGetProxy.JAVA還是本文的關鍵部分:
[java] view plaincopyprint?
public class HttpGetProxy {
final static private String TAG = "HttpGetProxy";
final static private String LOCAL_IP_ADDRESS_1 = "127.0.0.1";
final static private String LOCAL_IP_ADDRESS_2 = "10.0.2.2";
final static private int HTTP_PORT = 80;
final static private String HTTP_END="\r\n\r\n";
/**代理服務器使用的端口*/
private int proxy_ip_port;
/**鏈接帶的端口*/
private String original_ip_port;
/**遠程服務器地址*/
private String remoteHost;
/**本地服務器地址*/
private String localHost;
private ServerSocket localServer = null;
/**收發Media Player請求的Socket*/
private Socket sckPlayer = null;
/**收發Media Server請求的Socket*/
private Socket sckServer = null;
private SocketAddress address;
/**
* 初始化代理服務器
* @param localport 代理服務器監聽的端口
*/
public HttpGetProxy(int localport) {
try {
_HttpGetProxy(LOCAL_IP_ADDRESS_1,localport);