HardBirch

Android MediaPlayer与Http Proxy结合之基础篇

时间:12-02-06 栏目:安卓入门与提高 作者:张飞不张,文采横飞 评论:18 点击: 6,495 次


       最近半年都忙着Android TV项目,在春节假期才有时间写点东西。先在这里给大家拜个年,祝大家龙年快乐...

       直接进入主题:本文将会教大家如何实现一个简单的代理服务器(仅支持Http Get),与Android的MediaPlayer结合,从而可以扩展出“播放 防盗链的媒体文件”,“边播放边保存”等的功能。

       本文的代码可以到这里下载:http://download.csdn.net/detail/hellogv/4047134,代码分为两个工程:

  1. J2SE工程:HttpGetProxy,在PC上实现简单的代理服务器,核心类是HttpGetProxy.java,非常容易使用,这里就不唠叨了直接贴出运行效果图:
  2. Android工程:本文重点,必须唠叨一下。MediaPlayer播放网络音频(http://blog.csdn.net/hellogv/article/details/6406732)与HttpGetProxy.java结合,通过代理服务器播放网络音频。

   接下来贴出HttpGetProxy.java的原理图:

 接下来贴出HttpGetProxy.java的源码:

通过RemoteSocket的out_remoteSocket可以访问防盗链资源,HttpGetProxy通过2个线程来实现转发,可以在两个线程内实现保存的功能。

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.UnknownHostException; import android.util.Log; public class HttpGetProxy { final private String LOCAL_IP_ADDRESS = "127.0.0.1"; final private int HTTP_PORT = 80; private ServerSocket localServer = null; private Socket localSocket = null; private Socket remoteSocket = null; private String remoteIPAddress; private InputStream in_remoteSocket; private OutputStream out_remoteSocket; private InputStream in_localSocket; private OutputStream out_localSocket; private interface OnFinishListener { void onFinishListener(); } public HttpGetProxy(int 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("---->","..........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(); } } }; public void startProxy(String remoteIpAddr) throws IOException { remoteIPAddress = remoteIpAddr; SocketAddress address = new InetSocketAddress(remoteIPAddress,HTTP_PORT); // --------连接目标服务器---------// remoteSocket = new Socket(); remoteSocket.connect(address); System.out.println("..........remote Server connected.........."); Log.e("---->","..........remote Server connected.........."); in_remoteSocket = remoteSocket.getInputStream(); out_remoteSocket = remoteSocket.getOutputStream(); System.out.println("..........init remote Server I/O.........."); /** * 接收本地request,并转发到远程服务器 */ new Thread() { public void run() { int bytes_read; byte[] local_request = new byte[5120]; try { // 本地Socket localSocket = localServer.accept(); System.out.println("..........localSocket connected.........."); Log.e("---->","..........localSocket connected.........."); in_localSocket = localSocket.getInputStream(); out_localSocket = localSocket.getOutputStream(); System.out.println("..........init local Socket I/O.........."); Log.e("---->","..........init local Socket I/O.........."); String buffer = ""; while ((bytes_read = in_localSocket.read(local_request)) != -1) { String str = new String(local_request); System.out.println("localSocket " + str); Log.e("localSocket---->",str); buffer = buffer + str; if (buffer.contains("GET") && buffer.contains("rnrn")) { //---把request中的本地ip改为远程ip---// buffer = buffer.replace(LOCAL_IP_ADDRESS,remoteIPAddress); System.out.println("已经替换IP"); out_remoteSocket.write(buffer.getBytes()); out_remoteSocket.flush(); continue; } else{ out_remoteSocket.write(buffer.getBytes()); out_remoteSocket.flush(); } } System.out.println("..........local finish receive..........."); Log.e("---->","..........local finish receive.........."); finishListener.onFinishListener(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); /** * 接收远程服务器reply,并转发到本地客户端 */ new Thread() { public void run() { int bytes_read; byte[] remote_reply = new byte[5120]; try { System.out.println("..........remote start to receive..........."); Log.e("---->","..........remote start to receive.........."); while ((bytes_read = in_remoteSocket.read(remote_reply)) != -1) { //System.out.println("remoteSocket " + remote_reply.length); //System.out.println("remoteSocket " + new String(remote_reply)); out_localSocket.write(remote_reply, 0, bytes_read); out_localSocket.flush(); } System.out.println("..........remote finish receive..........."); Log.e("---->","..........remote finish receive.........."); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); } } 

 

 

声明: 本文由( 张飞不张,文采横飞 )原创编译,转载请保留链接: Android MediaPlayer与Http Proxy结合之基础篇

Android MediaPlayer与Http Proxy结合之基础篇:目前有18 条留言

  1. 18楼
    make1828:

    你好,我是一个android的初级开发者,我的qq是:258341964 ,请加我,谢谢

    2012-02-06 15:03 [回复]
  2. 17楼
    s278777851:

    人才, 顶上

    2012-02-06 15:33 [回复]
  3. 16楼
    yiyaaixuexi:

    "—->" 我也经常用这个tag

    2012-02-06 15:33 [回复]
  4. 15楼
    hmc1985:

    不错!

    2012-02-06 19:43 [回复]
  5. 14楼
    hellogv:

    [reply]yiyaaixuexi[/reply]
    学做饭吧,别写代码了

    2012-02-06 20:15 [回复]
  6. 13楼
    qq1761310972:

    [reply]hellogv[/reply]
    美女搞IT

    2012-02-06 21:05 [回复]
  7. 12楼
    yiyaaixuexi:

    [reply]hellogv[/reply]
    行啊,你给我发工资啊

    2012-02-06 23:13 [回复]
  8. 11楼
    hellogv:

    [reply]yiyaaixuexi[/reply]
    可以啊,专职煮饭

    2012-02-07 11:43 [回复]
  9. 10楼
    amheaven1121:

    gv,肥鱼来了.顶一个先.

    2012-02-07 15:15 [回复]
  10. 终于看到你发新帖了呀!

    2012-02-08 14:21 [回复]
  11. 8楼
    A328240784:

    用代理是为了看youtube之类的视频?

    2012-02-08 19:19 [回复]
  12. 有事情问您 急需您的帮忙 关于pad内存溢出的问题,我频繁的打开activity dialog形式的,里面有一个gallery 回加载图片 可是频繁的打开关闭 内存就会不停的涨 而不释放 最后就会内存溢出导致程序崩溃 能帮下吗? 最好加下其他IM工具 谢谢了

    2012-02-14 11:45 [回复]
  13. 急 希望能快些回复 QQ 511250000

    2012-02-14 11:46 [回复]
  14. 5楼
    sugarsla:

    sop 一族 …

    2012-03-12 09:58 [回复]
  15. 顶起

    2012-03-12 11:23 [回复]
  16. 地板
    qq641376680:

    很好啊,继续努力哦

    2012-03-22 21:04 [回复]
  17. 板凳
    mmcq_:

    你好,防盗链这个我不太懂哦。呵呵,我也要加你QQ,看了你07年的博客,很有意思啊。这都12年了、不容易啊、还在写博客。加油~哈哈、、我的QQ:307407585

    2012-04-11 10:05 [回复]
  18. 沙发
    lovestevevai:

    感谢分享,先复制下来有时间时再慢慢看

    2012-06-12 15:46 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐