get duration of audio file

我怕爱的太早我们不能终老 提交于 2019-11-29 20:43:03
Jacky

MediaMetadataRetriever is a lightweight and efficient way to do this. MediaPlayer is too heavy and could arise performance issue in high performance environment like scrolling, paging, listing, etc.

Furthermore, Error (100,0) could happen on MediaPlayer since it's a heavy and sometimes restart needs to be done again and again.

Uri uri = Uri.parse(pathStr);
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(AppContext.getAppContext(),uri);
String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
int millSecond = Integer.parseInt(durationStr);

Either try this to get duration in milliseconds:

MediaPlayer mp = MediaPlayer.create(yourActivity, Uri.parse(pathofyourrecording));
int duration = mp.getDuration();

Or measure the time elapsed from recorder.start() till recorder.stop() in nanoseconds:

long startTime = System.nanoTime();    
// ... do recording ...    
long estimatedTime = System.nanoTime() - startTime;

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.

The quickest way to do is via MediaMetadataRetriever. However, there is a catch

if you use URI and context to set data source you might encounter bug https://code.google.com/p/android/issues/detail?id=35794

Solution is use absolute path of file to retrieve metadata of media file.

Below is the code snippet to do so

 private static String getDuration(File file) {
                MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
                mediaMetadataRetriever.setDataSource(file.getAbsolutePath());
                String durationStr = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                return Utils.formateMilliSeccond(Long.parseLong(durationStr));
            }

Now you can convert millisecond to human readable format using either of below formats

     /**
         * Function to convert milliseconds time to
         * Timer Format
         * Hours:Minutes:Seconds
         */
        public static String formateMilliSeccond(long milliseconds) {
            String finalTimerString = "";
            String secondsString = "";

            // Convert total duration into time
            int hours = (int) (milliseconds / (1000 * 60 * 60));
            int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
            int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);

            // Add hours if there
            if (hours > 0) {
                finalTimerString = hours + ":";
            }

            // Prepending 0 to seconds if it is one digit
            if (seconds < 10) {
                secondsString = "0" + seconds;
            } else {
                secondsString = "" + seconds;
            }

            finalTimerString = finalTimerString + minutes + ":" + secondsString;

    //      return  String.format("%02d Min, %02d Sec",
    //                TimeUnit.MILLISECONDS.toMinutes(milliseconds),
    //                TimeUnit.MILLISECONDS.toSeconds(milliseconds) -
    //                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds)));

            // return timer string
            return finalTimerString;
        }

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

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

If the audio is from url, just wait for on prepared:

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
             length = mp.getDuration();
        }
});

It's simply. use RandomAccessFile Below is the code snippet to do so

 public static int getAudioInfo(File file) {
    try {
        byte header[] = new byte[12];
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        randomAccessFile.readFully(header, 0, 8);
        randomAccessFile.close();
        return (int) file.length() /1000;
    } catch (Exception e) {
        return 0;
    }
}

You can, of course, be more complete depending on your needs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!