访问媒体播放器缓存

Rah*_*rma 17 android android-mediaplayer

一旦完全加载,我想将逐步流式播放的mp3文件移动到SD卡.有没有办法实现这一目标.

我已经看到MediaPlayer完全下载整个文件,而渐进式流媒体,然后我们可以寻找文件的任何部分.我想将完全流式文件移动到外部存储器,以便将来播放不会浪费数据和电池.

Asa*_*ssi 16

我们的想法是创建一个媒体播放器可以读取的代理,而不是直接从Web上读取数据.

我使用了danikula/AndroidVideoCache,它非常易于构建/使用.我把它用于音频而不是视频,但它也是一样的.

  • 这应该是正确的答案。直接且易于使用。我希望我能投 10 票。 (2认同)

Dav*_*ave 10

对原帖的评论指出了你正确的方向,但我认为解释一下可能会有所帮助......

我所做的是使用Apache HTTP库构建一个轻量级代理服务器.应该有大量的例子来获得这部分的基础知识.为MediaPlayer提供适当的localhost URL,以便它为您的代理打开一个套接字.当MediaPlayer发出请求时,使用代理向实际媒体主机发送等效请求.您将在代理的packetReceived方法中接收byte []数据,我用它来构建HttpGet并使用AndroidHttpClient发送它.

您将获得一个HttpResponse,您可以使用内部的HttpEntity来访问流式字节数据.我正在使用ReadableByteChannel,如下所示:

HttpEntityWrapper entity = (HttpEntityWrapper)response.getEntity();
ReadableByteChannel src = Channels.newChannel(entity.getContent());
Run Code Online (Sandbox Code Playgroud)

在您回读数据时执行您想要的任何数据(例如将其缓存在SD卡上的文件中).要将正确的内容传递给MediaPlayer,从客户端Socket获取SocketChannel,首先将响应头直接写入该通道,然后继续写入实体的字节数据.我在while循环中使用NIO ByteBuffer(客户端是Socket,缓冲区是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
    }
}
Run Code Online (Sandbox Code Playgroud)

您可能需要在将响应传递给播放器之前更改响应中的主机标头,以使其看起来像您的代理是发送方,但我正在处理MediaPlayer的专有实现,因此行为可能会有所不同.希望有所帮助.

  • 你能分享一些关于如何创建代理和拦截数据的更多代码吗? (2认同)

Har*_*shi 5

已经晚了,但我发现大多数人仍然需要解决方案。我的解决方案基于JakeWharton 的 DiskLruCache。我们需要两件事

  • AsyncTask 读取文件或从网络下载并缓存它

  • 从缓存中获取 InputStram/FileDescriptor 的回调

第1步:

import android.content.Context;
import android.os.AsyncTask;
import org.apache.commons.io.IOUtils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

// you can use FileDescriptor as 
// extends AsyncTask<String, Void, FileDescriptor>

public class AudioStreamWorkerTask extends AsyncTask<String, Void, FileInputStream> {

    private OnCacheCallback callback = null;
    private Context context = null;

    public AudioStreamWorkerTask(Context context, OnCacheCallback callback) {
        this.context = context;
        this.callback = callback;
    }

    @Override
    protected FileInputStream doInBackground(String... params) {
        String data = params[0];
        // Application class where i did open DiskLruCache
        DiskLruCache cache = MyApplication.getDiskCache(context);
        if (cache == null)
            return null;
        String key = hashKeyForDisk(data);
        final int DISK_CACHE_INDEX = 0;
        long currentMaxSize = cache.getMaxSize();
        float percentageSize = Math.round((cache.size() * 100.0f) / currentMaxSize);
        if (percentageSize >= 90) // cache size reaches 90%
            cache.setMaxSize(currentMaxSize + (10 * 1024 * 1024)); // increase size to 10MB
        try {
            DiskLruCache.Snapshot snapshot = cache.get(key);
            if (snapshot == null) {
                Log.i(getTag(), "Snapshot is not available downloading...");
                DiskLruCache.Editor editor = cache.edit(key);
                if (editor != null) {
                    if (downloadUrlToStream(data, editor.newOutputStream(DISK_CACHE_INDEX)))
                        editor.commit();
                    else
                        editor.abort();
                }
                snapshot = cache.get(key);
            } else
                Log.i(getTag(), "Snapshot found sending");
            if (snapshot != null)
                return (FileInputStream) snapshot.getInputStream(DISK_CACHE_INDEX);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(getTag(), "File stream is null");
        return null;
    }

    @Override
    protected void onPostExecute(FileInputStream fileInputStream) {
        super.onPostExecute(fileInputStream);
        if (callback != null) {
            if (fileInputStream != null)
                callback.onSuccess(fileInputStream);
            else
                callback.onError();
        }
        callback = null;
        context = null;
    }

    public boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
        HttpURLConnection urlConnection = null;
        try {
            final URL url = new URL(urlString);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream stream = urlConnection.getInputStream();
            // you can use BufferedInputStream and BufferOuInputStream
            IOUtils.copy(stream, outputStream);
            IOUtils.closeQuietly(outputStream);
            IOUtils.closeQuietly(stream);
            Log.i(getTag(), "Stream closed all done");
            return true;
        } catch (final IOException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null)
                IOUtils.close(urlConnection);
        }
        return false;
    }

    private String getTag() {
        return getClass().getSimpleName();
    }

    private String hashKeyForDisk(String key) {
        String cacheKey;
        try {
            final MessageDigest mDigest = MessageDigest.getInstance("MD5");
            mDigest.update(key.getBytes());
            cacheKey = bytesToHexString(mDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            cacheKey = String.valueOf(key.hashCode());
        }
        return cacheKey;
    }

    private String bytesToHexString(byte[] bytes) {
        // http://stackoverflow.com/questions/332079
        StringBuilder sb = new StringBuilder();
        for (byte aByte : bytes) {
            String hex = Integer.toHexString(0xFF & aByte);
            if (hex.length() == 1)
                sb.append('0');
            sb.append(hex);
        }
        return sb.toString();
    }
}
Run Code Online (Sandbox Code Playgroud)

第2步:

public interface OnCacheCallback {

    void onSuccess(FileInputStream stream);

    void onError();
}
Run Code Online (Sandbox Code Playgroud)

例子

final String path = "http://www.example.com/test.mp3";
new AudioStreamWorkerTask (TestActivity.this, new OnCacheCallback() {

@Override
public void onSuccess(FileInputStream fileInputStream) {
    Log.i(getClass().getSimpleName() + ".MediaPlayer", "now playing...");
    if (fileInputStream != null) {
        // reset media player here if necessary
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(fileInputStream.getFD());
            mediaPlayer.prepare();
            mediaPlayer.setVolume(1f, 1f);
            mediaPlayer.setLooping(false);
            mediaPlayer.start();
            fileInputStream.close();
        } catch (IOException | IllegalStateException e) {
            e.printStackTrace();
        }
    } else {
        Log.e(getClass().getSimpleName() + ".MediaPlayer", "fileDescriptor is not valid");
    }
}

@Override
public void onError() {
    Log.e(getClass().getSimpleName() + ".MediaPlayer", "Can't play audio file");
}
}).execute(path);
Run Code Online (Sandbox Code Playgroud)

笔记:

这是经过测试的音频文件缓存的粗略示例,如果您发现任何问题,可能会出现一些问题,请通知我:)

  • DiskLruCache diskLruCache = DiskLruCache.open(getCacheDir(), 1, 1, 50 * 1024 * 1024); (2认同)