开发者

Wrong duration of sound file on android 2.2

开发者 https://www.devze.com 2023-03-07 15:16 出处:网络
I am developing application in which there is a list of some audio files. When I click on a item it plays the corresponding audio and also shows the duration. The problem is in android 2.1 device and

I am developing application in which there is a list of some audio files. When I click on a item it plays the corresponding audio and also shows the duration. The problem is in android 2.1 device and emulator the duration is correct but in android 2.2 emulator it's showing wrong duration. Does anyone have ide开发者_运维百科a to solve the problem. Is there a good method to get the correct duration of the sound files. The audio files are in the res/raw folder. And one thing for the same sounds iphone is showing correct duration.


Yes

It is probably due to VBR files. Variable bit rates mention a rate in the header, which is probably used by Android software to calculate the duration of the MP3 from it's length.

I remember having seen a utility that can calculate a 'correct' effective bitrate and prefix a separate MP3 data frame at the start just to make it report the 'correct' (average) bitrate.

Try VBRFix

Throughout a song there are points that require high quality and points that require low quality(i.e. silence). Instead of having the whole file at one quality: VBR(Variable Bit Rate) provides us with a variable quality within the file. This allows us to more efficiently use the file space. The problem is that many MP3 playing programs estimate the time of a MP3 based on the first bitrate they find and the file size. Also, when jumping through a file the positions aren't the same - half way through a VBR mp3 may not be half way through the song. Ogg Vorbis is a more advanced free music format and uses VBR as default without problem

It is also in the repositories for Ubuntu (Debian likely): sudo apt-get install vbrfix


I might be a bit late on answering this but, anyway I was able fix a problem of a similar kind using Ringdroid. This is what you'd have to do to get the audio duration in milliseconds from VBR files using Rindroid

public class AudioUtils
{
    public static long getDuration(CheapSoundFile cheapSoundFile)
    {
        if( cheapSoundFile == null)
            return -1;
        int sampleRate = cheapSoundFile.getSampleRate();
        int samplesPerFrame = cheapSoundFile.getSamplesPerFrame();
        int frames = cheapSoundFile.getNumFrames();
        cheapSoundFile = null;
        return 1000 * ( frames * samplesPerFrame) / sampleRate;
    }

    public static long getDuration(String mediaPath)
    {
        if( mediaPath != null && mediaPath.length() > 0)
            try 
            {
                return getDuration(CheapSoundFile.create(mediaPath, null));
            }catch (FileNotFoundException e){} 
            catch (IOException e){}
        return -1;
    }
}

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消