get duration of audio file

后端 未结 10 1161
生来不讨喜
生来不讨喜 2020-12-07 19:15

I have made a voice recorder app, and I want to show the duration of the recordings in a listview. I save the recordings like this:

MediaRecorder recorder =          


        
10条回答
  •  鱼传尺愫
    2020-12-07 19:46

    Have you looked at Ringdroid?. It's pretty light weight and the integration is straight forward. It works well with VBR media files as well.

    For your problem with getting the duration, you might want to do something like below using Ringdroid.

    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 that helps

提交回复
热议问题