Create a proxy server in android to intercept media player request and response

2020-12-16 22:06:12

参考地址 Create a proxy server in android to intercept media player request and response


1

I am trying to extract ID3 tags from live streams on an android device. Extraction of ID3 tags from live streams in not available in android by default.

The approach I want to follow is to intercept the media player request and response in a proxy server and analyze the byte data to extract ID3 tags. access to media player cache gives us a rough idea to achieve this, but I am not sure if we will be able to proxy the media player request/response. If anyone has done this or has any pointers on it, it would be very helpful ...

Thanks

   

 

创建 30 12月. 13 

2

You have to provide the MediaPlayer a localhost address and listen on the port you specify. For instance, if you have set up your proxy server to listen on port 8090, you might set the data source like:

mp.setDataSource(whateverContext, Uri.parse("http://127.0.0.1:8090"));

You will then receive requests from the MediaPlayer to which you have to respond. In order to respond properly, you simply forward the request to the remote media server (the original URI). To keep it simple you can use AndroidHttpClient to make the requests. When you get responses from the remote server, you need to write that data to the socket opened by the MediaPlayer, first the HTTP headers, followed by the entity's binary data when that is relevant.

Edit:

I haven't looked at this project very much, but it is oft mentioned as the archetypal project for using a proxy with the MediaPlayer. Their class that does the proxy work looks to be StreamProxy.java. They've used DefaultHttpClient instead of AndroidHttpClient, but it's basically the same as I described. Notice in particular their processRequest method. The following contains excerpts from that method with my comments:

// Execute the HttpRequest and receive an HttpResponseHttpResponse realResponse = download(url);// ...// Retrieve the response entity as an InputStreamInputStream data = realResponse.getEntity().getContent();// ...try {
       // The response headers have been concatenated into httpString,
       // so write the headers to the client socket
       byte[] buffer = httpString.toString().getBytes();
       client.getOutputStream().write(buffer, 0, buffer.length);
       // Write the entity data to the client socket (repeatedly read from
       // the entity stream and write to the client socket)
       byte[] buff = new byte[1024 * 50];
       while (isRunning && (readBytes = data.read(buff, 0, buff.length)) != -1) {
           client.getOutputStream().write(buff, 0, readBytes);
       }} // ...

 



  • 2018-10-20 20:44:03

    RecycleView4种定位滚动方式演示

    将RecyclerView滑动到指定位置,或者检索RecyclerView的某一项(各个项的高度不确定),然后定位滚动这到一项,将它显示。 下面就讲解4种RecyclerView定位滚动

  • 2018-10-21 22:29:24

    android ScollView 嵌套 WebView 底部空白,高度无法自适应解决

    最近要做一个页面,需要 ScrollView 嵌套 WebView,怎么嵌套,怎么解决焦点和 touch 事件冲突,网上一大堆,这里就不赘述了,但是发现 WebView 从一个高度很高的网页加载一个高度很低的网页的时候,高度无法自适应了,造成底部会有一大片的空白,解决方案找到了挺多,描述一下:

  • 2018-10-24 15:24:54

    getcachedir和getexternalcachedir的区别

    应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的。 大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中。 这样当该应用被卸载后,这些数据还保留在SDCard中,留下了垃圾数据。 如果你想让你的应用被卸载后,与该应用相关的数据也清除掉,该怎么办呢?