get duration of audio file

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

    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

提交回复
热议问题