问题
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