解码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;
}
最佳答案
您上面列出的decode
方法只是示例代码。您不应该在生产中使用它,也就是说,您正在传递一个路径并反复重新打开同一个文件,这是一项代价高昂的操作。
相反,您应该在此方法之外打开文件,将其打开到 InputStream 中,然后将 InputStream 传递到该方法中。有关示例,请参见此问题:Android JellyBean network media issue
关于android - 使用jlayer在Android上进行慢速MP3解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14645087/