package com.musicplayer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URI;
import java.net.UnknownHostException;
import android.util.Log;
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();
}
/**
* 初始化代理服务器
* @param localport 代理服务器监听的端口
*/
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();
}
}
/**
* 结束时,清除所有资源
*/
private OnFinishListener finishListener = new OnFinishListener() {
@Override
public void onFinishListener() {
System.out.println("..........release all..........");
Log.e(TAG, "..........release 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();
}
}
};
/**
* 把网络URL转为本地URL,127.0.0.1替换网络域名
* @param url 网络URL
* @return 本地URL
*/
public String getLocalURL(String url){
String result = null;
URI originalURI=URI.create(url);
remoteHost=originalURI.getHost();
if(originalURI.getPort()!=-1){//URL带Port
address = new InetSocketAddress(remoteHost,
originalURI.getPort());//使用默认端口
result=url.replace(remoteHost+":"+originalURI.getPort(),
LOCAL_IP_ADDRESS+":"+local_ip_port);
}
else{//URL不带Port
address = new InetSocketAddress(remoteHost,
HTTP_PORT);//使用80端口
result=url.replace(remoteHost,LOCAL_IP_ADDRESS+":"+local_ip_port);
}
return result;
}
/**
* 启动代理服务器
* @throws IOException
*/
public void startProxy() 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 {
//--------------------------------------
//监听MediaPlayer的请求,MediaPlayer->代理服务器
//--------------------------------------
localSocket = localServer.accept();
Log.e(TAG, "..........localSocket connected..........");
in_localSocket = localSocket.getInputStream();
out_localSocket = localSocket.getOutputStream();
Log.e(TAG, "..........init local Socket I/O..........");
String buffer = "";//保存MediaPlayer的HTTP请求
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")) {
// ---把request中的本地ip改为远程ip---//
buffer = buffer.replace(LOCAL_IP_ADDRESS,remoteHost);
break;
}
}
Log.e(TAG, "..........local finish receive..........");
//--------------------------------------
//把MediaPlayer的请求发到网络服务器,代理服务器->网络服务器
//--------------------------------------
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());//发送MediaPlayer的请求
out_remoteSocket.flush();
//------------------------------------------------------
//把网络服务器的反馈发到MediaPlayer,网络服务器->代理服务器->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();//释放资源
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android MediaPlayer与Http Proxy结合之提高篇
共50个文件
class:32个
java:4个
xml:3个
需积分: 31 7 下载量 33 浏览量
2013-11-22
14:40:25
上传
评论
收藏 65KB RAR 举报
温馨提示
代理服务器可以增强MediaPlayer对复杂的Http情况的适应,可以播放带防盗链的媒体文件,边播边存,还可以对大体积的媒体文件(如视频)进行多线程预加载,达到快速播放的效果。
资源推荐
资源详情
资源评论
收起资源包目录
musicplayer_demo.rar (50个子文件)
musicplayer_demo
bin
classes.dex 13KB
res
drawable
icon.png 2KB
com
musicplayer
Player$1.class 963B
WebServer.class 3KB
R$layout.class 385B
CallBack.class 420B
test_musicplayer$ClickEvent.class 1KB
R$id.class 477B
R$drawable.class 391B
test_musicplayer$SeekBarChangeEvent.class 1KB
R$attr.class 331B
HttpConstants.class 1KB
Worker.class 8KB
R.class 510B
Player.class 4KB
test_musicplayer.class 4KB
R$string.class 418B
Player$2.class 1KB
MusicPlayer.apk 13KB
classes
com
musicplayer
Player$1.class 963B
HttpGetProxy$OnFinishListener.class 255B
HttpGetProxy$1.class 2KB
R$layout.class 385B
test_musicplayer$ClickEvent.class 1KB
R$id.class 414B
R$drawable.class 391B
test_musicplayer$SeekBarChangeEvent.class 1KB
R$attr.class 331B
R.class 510B
HttpGetProxy.class 4KB
Player.class 3KB
test_musicplayer.class 2KB
R$string.class 418B
Player$2.class 1KB
HttpGetProxy$2.class 4KB
resources.ap_ 5KB
res
drawable-ldpi
drawable-hdpi
drawable
icon.png 3KB
values
strings.xml 173B
drawable-mdpi
layout
main.xml 1KB
assets
default.properties 391B
gen
com
musicplayer
R.java 793B
.settings
org.eclipse.core.resources.prefs 165B
org.eclipse.jdt.core.prefs 629B
src
com
musicplayer
test_musicplayer.java 2KB
HttpGetProxy.java 5KB
Player.java 3KB
.project 847B
.classpath 288B
project.properties 391B
AndroidManifest.xml 756B
共 50 条
- 1
资源评论
Huangdroid
- 粉丝: 37
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功