解码10秒需要1分钟,如何更快地解码MP3?
public static byte[] decode(String path, int startMs, int maxMs) throws FileNotFoundException
{
float totalMs = 0;
ByteArrayOutputStream os = new ByteArrayOutputStream();
File file = new File(path);
InputStream inputStream = new BufferedInputStream(new FileInputStream(file), 8 * 1024);
try {
Bitstream bitstream = new Bitstream(inputStream);
Decoder decoder = new Decoder();
boolean done = false;
while (! done) {
Header frameHeader = bitstream.readFrame();
totalMs += frameHeader.ms_per_frame();
SampleBuffer output = (SampleBuffer) decoder.decodeFrame(frameHeader, bitstream);
short[] pcm = output.getBuffer();
for (short s : pcm) {
os.write(s & 0xff);
os.write((s >> 8 ) & 0xff);
}
if (totalMs >= (startMs + maxMs)) {
done = true;
}
bitstream.closeFrame();
}
return os.toByteArray();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
答案 0 :(得分:1)
您上面列出的decode
方法只是示例代码。你不应该在生产中使用它,也就是说,你正在传递路径并重复重复打开同一个文件,这是一项代价高昂的操作。
相反,您应该将此方法之外的文件打开到InputStream中,然后将InputStream传递给方法。请参阅此问题以获取示例:Android JellyBean network media issue