1

解码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;    
}
4

1 回答 1

1

您上面列出的decode方法只是示例代码。您不应该在生产中使用它,也就是说,您要传递一个路径并反复重新打开同一个文件,这是一项代价高昂的操作。

相反,您应该在此方法之外将文件打开到 InputStream,然后将 InputStream 传递给该方法。看这个问题的例子:Android JellyBean network media issue

于 2013-03-03T00:06:19.513 回答