使用jlayer在Android上进行慢速MP3解码 [英] Slow MP3 decoding on Android using jlayer

查看:99
本文介绍了使用jlayer在Android上进行慢速MP3解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解码10秒需要1分钟,如何才能更快地解码MP3?

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

推荐答案

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

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.

相反,您应该在此方法之外将文件打开到InputStream中,然后将InputStream传递到该方法中.参见以下示例示例: Android JellyBean网络媒体问题

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

这篇关于使用jlayer在Android上进行慢速MP3解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆