Slow MP3 decoding on Android using jlayer

你说的曾经没有我的故事 提交于 2019-12-24 10:56:40

问题


It require 1 minute to decode 10 seconds, how can I decode the MP3 faster?

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;    
}

回答1:


The decode method you have listed above is just sample code. You shouldn't be using it in production, namely, you're passing a path and reopening the same file repeatedly, a costly operation.

Instead, you should open the file outside of this method, into an InputStream, and then pass the InputStream into the method. See this question for an example: Android JellyBean network media issue



来源:https://stackoverflow.com/questions/14645087/slow-mp3-decoding-on-android-using-jlayer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!