Load sound from memory in Android

Is there any way to load a sound samples from memory using SoundPool.load method?

Unfortunatelly, all methods provided in SoundPool are using arguments for real files. The problem is that I want to load sounds from zip file on SDcard, and extracting zip (like in this solution) is not an option.

Furthermore, there is a solution for loading from uncomressed zip files, but my files is comressed (and will be with password).

So is there any way to have java.io.FileDescriptor that represents a virtual file, so I can implement some abstract class placing my own streams?

Best regards.


I got the final answer on this question. This is feature-missing on Android platform. Android media playback framework doesn't support it for a very long time. Google also notices it, so Android6.0(API Level23) introduces android.media.MediaDataSource which could be used as a pure memory byte-array data source. Before API Level23, we still need to copy the memory data to a temporary file in the file system.

The following URL provide some more clues on this issue, its explanation is correct for both audio and video: how to play video from byte array in media player


I wanted to suggest you to use MemoryFile, but after checking it, I found the MemoryFile has no getFileDescriptor() method, it means we couldn't use it as a parameter in SoundPool.load(). But I found this post: what is the use of MemoryFile in android

One guy implemented MemoryFileUtil.getFileDescriptor(memFile), he posts his codes there. If his codes really could work, it means we could load a sound sample from memory using SoundPool.load(). The only problem is writing our memory data into that memory file. The following site shows how to write memory data(from SQLITE query result) into a memory file: http://www.programcreek.com/java-api-examples/index.php?api=android.os.MemoryFile

I'll give you updates after my test.

链接地址: http://www.djcxy.com/p/23484.html

上一篇: 使用正则表达式解析VBA Const声明...

下一篇: 在Android中加载内存中的声音