The combination of Android MediaPlayer with HTTP proxy

Source: Internet
Author: User

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

The basic implementation of a simple proxy server with the Android MediaPlayer combination (only support HTTP GET), through the proxy server to forward MediaPlayer request and Transport server response, But the basic text does not support seek, and this time, the improvement article supports seek. The proxy server can enhance the MediaPlayer to the complex HTTP condition adaptation, may play with the anti-theft chain the media file, the side broadcasts the side to save, also may to the large volume media file (for example video) carries on the multithread preload, achieves the fast playback the effect.

This article runs on the emulator, using the Microsoft Network Monitor 3.4来 grab packet, which can be found by grasping the package to reconnect to the server and add a range field to the HTTP GET request. Therefore, every time the proxy server hears MediaPlayer request, it needs a new socket to connect to the remote server.

The code of this article can go to http://download.csdn.net/detail/hellogv/4332362 download, this article program operation effect as shown in figure:

Next, post 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_remotesocket;
	Private InputStream In_localsocket;

	Private OutputStream Out_localsocket;
	Private socketaddress address;
	Private interface Onfinishlistener {void Onfinishlistener (); 
		/** * Initialize Proxy server * @param localport proxy Server listening on port * * * 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 block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block			E.printstacktrace (); }/** * At the end, clear all resources/private Onfinishlistener Finishlistener = new Onfinishlistener () {@Override public V
			OID Onfinishlistener () {System.out.println (.... ........... ...);
			LOG.E (TAG, "...)," and "the release of All ...");
				try {in_localsocket.close ();

				Out_remotesocket.close ();
				In_remotesocket.close ();

				Out_localsocket.close ();
				Localsocket.close ();
			Remotesocket.close ();
			catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();


	}
		}
	};
		/** * Convert network URL to 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 port = new Inetsocketaddress (RemoteHost, Originaluri.getport ());//Use Default Port Result=url.replace (remotehost+ ":" +originaluri.getport (), local_ip_address+ ":" +loCal_ip_port); Else{//url does not take port address = new Inetsocketaddress (remotehost, http_port);//Use 80 port result=url.replace (Remoteh
		ost,local_ip_address+ ":" +local_ip_port);
		
	return result; /** * Start Proxy Server * @throws ioexception/public void Startproxy () throws IOException {new Thread () {PU
				Blic void Run () {int bytes_read;
				byte[] local_request = new byte[1024];
				byte[] remote_reply = new byte[1024];
						while (true) {try {//--------------------------------------//Listen MediaPlayer request,mediaplayer-> Proxy Server

						--------------------------------------Localsocket = localserver.accept ();
						LOG.E (TAG, "... localsocket connected ...)."-------
						In_localsocket = Localsocket.getinputstream ();
						Out_localsocket = Localsocket.getoutputstream ();

						LOG.E (TAG, "...) init local Socket I/o ..."); and the other. String buffer = "";//Save 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\r\n")) {//---Change the local IP in request to a remote IP---//
								Buffer = Buffer.replace (local_ip_address,remotehost);
							Break

						} log.e (TAG, "...)." Local finish receive .......... --------------------------------------//Send the MediaPlayer request to the network server, the proxy server-> the network server//--------------------------
						------------remotesocket = new Socket ();
						Remotesocket.connect (address);
						LOG.E (TAG, "...) remote Server connected ..."); and the other is a.
						In_remotesocket = Remotesocket.getinputstream ();
						Out_remotesocket = Remotesocket.getoutputstream ();

						Out_remotesocket.write (Buffer.getbytes ());//Send MediaPlayer request Out_remotesocket.flush (); ------------------------------------------------------//Send the feedback of the network server 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 ()//Free resource} catch (IOException e) {//TODO auto-generated catch block E.P
					Rintstacktrace ();
	}}}.start (); }}  

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.