媒体播放器缓存中获取访问 [英] Getting access to media player cache

查看:135
本文介绍了媒体播放器缓存中获取访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搬到一个渐进流MP3文件到SD卡上,一旦它被完全加载。有没有办法实现这一目标的。

I want to move a progressively streamed mp3 file to sd card once it is completely loaded. Is there any way of achieving that.

我已经看到了的MediaPlayer 完全下载整个文件,而渐进式流,然后我们可以寻求任何部分文件。我想一个完全流文件移动到外部存储,以便日后播放不浪费数据和电池。

I've seen that the MediaPlayer completely downloads the whole file while progressive streaming and then we can seek to any part of file. I want to move a fully streamed file to external storage so that future playback do not waste data and battery.

推荐答案

在原来的岗位点,你在正确的方向上的评论,但我认为它可能有助于阐明有点...

The comment on the original post points you in the right direction, but I thought it may be helpful to expound a bit...

我所做的就是建立使用娜迦和Apache HTTP库一个轻量级的代理服务器。应该有大量的例子在那里获得这部分的基础知识。提供的MediaPlayer合适的本地主机地址,这样它会打开一个套接字来代理。在MediaPlayer发出请求时,使用代理来发送一个等价请求到实际的媒体主机。您将收到byte []的数据在代理的packetReceived方法,我用它来建立一个HTTPGET,它的道路上与AndroidHttpClient发送。

What I've done is build a lightweight proxy server using Naga and the Apache HTTP libraries. There should be plenty of examples out there to get the basics of this part. Provide the MediaPlayer an appropriate localhost URL so that it opens a socket to your proxy. When the MediaPlayer makes a request, use the proxy to send an equivalent request to the actual media host. You will receive byte[] data in the proxy's packetReceived method, which I use to build an HttpGet and send it on its way with AndroidHttpClient.

您会回来的Htt的presponse,你可以使用HttpEntity内部访问流式字节的数据。我使用的ReadableByteChannel,像这样:

You will get back an HttpResponse and you can use the HttpEntity inside to access the streaming byte data. I'm using a ReadableByteChannel, like so:

HttpEntityWrapper entity = (HttpEntityWrapper)response.getEntity();
ReadableByteChannel src = Channels.newChannel(entity.getContent());

做任何你想要的数据,你读回(就像它缓存在SD卡上的文件)。要通过正确的东西,到MediaPlayer的,从客户端的Socket得到的SocketChannel,先直接写响应头到该频道,然后继续写实体的字节数据。我在循环使用NIO的ByteBuffer(客户端是一个Socket和缓冲区是一个ByteBuffer的)。

Do whatever you'd like with the data as you read it back (like cache it in a file on the SD card). To pass the correct stuff on to the MediaPlayer, get the SocketChannel from the client Socket, first write the response headers directly to that channel, and then proceed to write the entity's byte data. I'm using an NIO ByteBuffer in a while loop (client is a Socket and buffer is a ByteBuffer).

int read, written;
SocketChannel dst = client.getChannel();
while (dst.isConnected() &&
    dst.isOpen() &&
    src.isOpen() &&
    (read = src.read(buffer)) >= 0) {
    try {
        buffer.flip();
        // This is one point where you can access the stream data.
        // Just remember to reset the buffer position before trying
        // to write to the destination.
        if (buffer.hasRemaining()) {
            written = dst.write(buffer);
            // If the player isn't reading, wait a bit.
            if (written == 0) Thread.sleep(15);
            buffer.compact();
        }
    }
    catch (IOException ex) {
        // handle error
    }
}

您可能需要沿着玩家传递之前,将改变在响应主机头,使它看起来像你的代理是发送者,但我处理与专有实施的MediaPlayer,这样的行为可能会有点不同。希望有所帮助。

You may need to alter the host header in the response before passing it along to the player so that it looks like your proxy is the sender, but I'm dealing with a proprietary implementation of the MediaPlayer so behaviour could be a bit different. Hope that helps.

这篇关于媒体播放器缓存中获取访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆