简体   繁体   中英

Slow MP3 decoding on Android using jlayer

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM