get duration of audio file

后端 未结 10 1122
生来不讨喜
生来不讨喜 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:43

    Try use

    long totalDuration = mediaPlayer.getDuration(); // to get total duration in milliseconds
    
    long currentDuration = mediaPlayer.getCurrentPosition(); // to Gets the current playback position in milliseconds
    

    Division on 1000 to convert to seconds.

    Hope this helped you.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-07 19:47

    You can use this readyMade method, hope this helps someone.

    Example 1 : getAudioFileLength(address, true); // if you want in stringFormat Example 2 : getAudioFileLength(address, false); // if you want in milliseconds

    public String getAudioFileLength(String path, boolean stringFormat) {
    
                Uri uri = Uri.parse(path);
                MediaMetadataRetriever mmr = new MediaMetadataRetriever();
                mmr.setDataSource(Filter_Journals.this, uri);
                String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                int millSecond = Integer.parseInt(duration);
    
                if (millSecond < 0) return String.valueOf(0); // if some error then we say duration is zero
    
                if (!stringFormat) return String.valueOf(millSecond);
    
                int hours, minutes, seconds = millSecond / 1000;
    
                hours = (seconds / 3600);
                minutes = (seconds / 60) % 60;
                seconds = seconds % 60;
    
                StringBuilder stringBuilder = new StringBuilder();
                if (hours > 0 && hours < 10) stringBuilder.append("0").append(hours).append(":");
                else if (hours > 0) stringBuilder.append(hours).append(":");
    
                if (minutes < 10) stringBuilder.append("0").append(minutes).append(":");
                else stringBuilder.append(minutes).append(":");
    
                if (seconds < 10) stringBuilder.append("0").append(seconds);
                else stringBuilder.append(seconds);
    
                return stringBuilder.toString();
            }
    
    0 讨论(0)
  • 2020-12-07 19:48

    After you write the file, open it up in a MediaPlayer, and call getDuration on it.

    0 讨论(0)
提交回复
热议问题