Improvement on the combination of Android mediaplayer and HTTP Proxy

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

The basic article combines a simple proxy server with Android mediaplayer (only http get is supported). You can use the proxy server to forward mediaplayer requests and transport server response, however, the basic article does not support seek. This article also supports seek. The proxy server can enhance mediaplayer's adaptability to complex HTTP conditions, play anti-leech media files, play and save, and play large volumes of media files (such as videos) multi-thread pre-loading for fast playback.

The code in this article runs on the simulator and uses Microsoft Network Monitor 3.4 to capture packets. By capturing packets, you can find that the seek operation reconnects to the server and adds the range field to the http get request, therefore, the proxy server needs to create a new socket to connect to the remote server each time it listens to the request of the mediaplayer.

The code in this article can be downloaded at http://download.csdn.net/detail/hellogv/4332362.

 

 

Next, paste the core code httpgetproxy. Java:

Public class httpgetproxy {final static private string tag = "httpgetproxy"; Final Static private string local_ip_address = "127.0.0.1"; Final Static private int http_port = 80; private int local_ip_port; private serversocket localserver = NULL; private socket localsocket = NULL; private socket remotesocket = NULL; private string remotehost; private inputstream in_remotesocket; private outputstream out_remo Tesocket; private inputstream in_localsocket; private outputstream out_localsocket; private socketaddress address; private interface onfinishlistener {void onfinishlistener ();} /*** initialize the proxy server * @ Param localport the port on which the proxy server listens */Public httpgetproxy (INT localport) {local_ip_port = localport; try {localserver = new serversocket (localport, 1, inetaddress. getbyname (local_ip_address);} catch (unknownhostexception E) {// Todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}/*** end, clear all resources */private onfinishlistener finishlistener = new onfinishlistener () {@ overridepublic void onfinishlistener () {system. out. println (".......... release all .......... "); log. E (TAG ,".......... release all .......... "); try {in_localsocket.close (); out_remo Tesocket. close (); in_remotesocket.close (); out_localsocket.close (); localsocket. close (); remotesocket. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}};/*** convert the network URL to a local URL, 127.0.0.1 replace network domain name * @ Param URL network URL * @ return local URL */Public String getlocalurl (string URL) {string result = NULL; Uri originaluri = Uri. create (URL); remotehost = originaluri. gethost (); If (originaluri. Getport ()! =-1) {// URL with portaddress = new inetsocketaddress (remotehost, originaluri. getport (); // use the default port result = URL. replace (remotehost + ":" + originaluri. getport (), local_ip_address + ":" + local_ip_port);} else {// URL without portaddress = new inetsocketaddress (remotehost, http_port); // use port 80 result = URL. replace (remotehost, local_ip_address + ":" + local_ip_port);} return result;}/*** start Proxy Server * @ throws ioexception */Public void startprox Y () throws ioexception {New thread () {public void run () {int bytes_read; byte [] local_request = new byte [1024]; byte [] remote_reply = new byte [1024]; while (true) {try {// ---------------------------------------- // listen to mediaplayer requests, mediaplayer-> Proxy Server // listen localsocket = localserver. accept (); log. E (TAG ,".......... localsocket connected .......... "); in_localsocket = localsock ET. getinputstream (); out_localsocket = localsocket. getoutputstream (); log. E (TAG ,".......... init local socket I/O .......... "); string buffer =" "; // Save the mediaplayer HTTP request while (bytes_read = in_localsocket.read (local_request ))! =-1) {string STR = new string (local_request); log. E ("localsocket ---->", STR); buffer = buffer + STR; If (buffer. contains ("get") & buffer. contains ("\ r \ n") {// --- change the local IP address in the request to a remote IP address --- // buffer = buffer. replace (local_ip_address, remotehost); break ;}} log. E (TAG ,".......... local finish receive .......... "); // -------------------------------------- // send the mediaplayer request to the network server, proxy server-> network server //----------------- --------------------- Remotesocket = new socket (); remotesocket. connect (Address); log. E (TAG ,".......... remote Server connected .......... "); in_remotesocket = remotesocket. getinputstream (); out_remotesocket = remotesocket. getoutputstream (); out_remotesocket.write (buffer. getbytes (); // send the mediaplayer request out_remotesocket.flush (); // -------------------------------------------------- // send the network server feedback to mediaplayer, Network Server> Proxy Server> mediaplayer // ---------------------------------------------------------- log. E (TAG ,".......... remote start to receive .......... "); While (bytes_read = in_remotesocket.read (remote_reply ))! =-1) {out_localsocket.write (remote_reply, 0, bytes_read); out_localsocket.flush ();} log. E (TAG ,".......... over .......... "); finishlistener. onfinishlistener (); // release resources} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}}. start ();}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.