简体   繁体   English

在android中创建代理服务器以拦截媒体播放器的请求和响应

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

I am trying to extract ID3 tags from live streams on an android device. 我正在尝试从Android设备上的实时流中提取ID3标签。 Extraction of ID3 tags from live streams in not available in android by default. 默认情况下,无法从实时流中提取ID3标签。

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. 我要遵循的方法是在代理服务器中拦截媒体播放器的请求和响应,并分析字节数据以提取ID3标签。 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 谢谢

You have to provide the MediaPlayer a localhost address and listen on the port you specify. 您必须为MediaPlayer提供一个localhost地址,并侦听您指定的端口。 For instance, if you have set up your proxy server to listen on port 8090, you might set the data source like: 例如,如果您已将代理服务器设置为侦听端口8090,则可以将数据源设置为:

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. 然后,您将收到来自MediaPlayer的请求,您必须对此做出响应。 In order to respond properly, you simply forward the request to the remote media server (the original URI). 为了正确响应,您只需将请求转发到远程媒体服务器(原始URI)。 To keep it simple you can use AndroidHttpClient to make the requests. 为简单起见,您可以使用AndroidHttpClient发出请求。 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. 从远程服务器获得响应时,需要将该数据写入由MediaPlayer打开的套接字,首先是HTTP标头,然后在相关时再写入实体的二进制数据。

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 . 我没有看过太多这个项目 ,但是它经常被提到作为与MediaPlayer一起使用代理的原型项目。 Their class that does the proxy work looks to be StreamProxy.java . 他们执行代理工作的类是StreamProxy.java They've used DefaultHttpClient instead of AndroidHttpClient , but it's basically the same as I described. 他们使用DefaultHttpClient代替了AndroidHttpClient ,但是基本上与我所描述的相同。 Notice in particular their processRequest method. 特别注意它们的processRequest方法。 The following contains excerpts from that method with my comments: 以下内容包含该方法的摘录,并附有我的评论:

// Execute the HttpRequest and receive an HttpResponse
HttpResponse realResponse = download(url);
// ...
// Retrieve the response entity as an InputStream
InputStream 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);
    }
} // ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM