派生自 projectDept/qhighschool

Administrator
2022-11-29 8c99e2d8b6c1e0d9cde6abbe80b4df75be19f6d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.qxueyou.scc.sys.utils;
 
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
 
 
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.File;
 
public class AudioUtil {
 
 
 
    /**
     * »ñÈ¡ÓïÒôÎļþ²¥·Åʱ³¤(Ãë) Ö§³Öwav ¸ñʽ
     * @param filePath
     * @return
     */
    public static Float getDuration(String filePath){
        try{
 
            File destFile = new File(filePath);
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(destFile);
            AudioFormat format = audioInputStream.getFormat();
            long audioFileLength = destFile.length();
            int frameSize = format.getFrameSize();
            float frameRate = format.getFrameRate();
            float durationInSeconds = (audioFileLength / (frameSize * frameRate));
            return durationInSeconds;
 
        }catch (Exception e){
            e.printStackTrace();
            return 0f;
        }
 
    }
 
    /**
     * »ñÈ¡mp3ÓïÒôÎļþ²¥·Åʱ³¤(Ãë) mp3
     * @param filePath
     * @return
     */
    public static Float getMp3Duration(String filePath){
 
        try {
            File mp3File = new File(filePath);
            MP3File f = (MP3File) AudioFileIO.read(mp3File);
            MP3AudioHeader audioHeader = (MP3AudioHeader)f.getAudioHeader();
            return Float.parseFloat(audioHeader.getTrackLength()+"");
        } catch(Exception e) {
            e.printStackTrace();
            return 0f;
        }
    }
 
 
    /**
     * »ñÈ¡mp3ÓïÒôÎļþ²¥·Åʱ³¤(Ãë)
     * @param mp3File
     * @return
     */
    public static Float getMp3Duration(File mp3File){
 
        try {
            //File mp3File = new File(filePath);
            MP3File f = (MP3File) AudioFileIO.read(mp3File);
            MP3AudioHeader audioHeader = (MP3AudioHeader)f.getAudioHeader();
            return Float.parseFloat(audioHeader.getTrackLength()+"");
        } catch(Exception e) {
            e.printStackTrace();
            return 0f;
        }
    }
 
 
    /**
     * µÃµ½pcmÎļþµÄºÁÃëÊý
     *
     * pcmÎļþÒôƵʱ³¤¼ÆËã
     * Í¬Í¼ÏñbmpÎļþÒ»Ñù£¬pcmÎļþ±£´æµÄÊÇδѹËõµÄÒôƵÐÅÏ¢¡£ 16bits ±àÂëÊÇÖ¸£¬Ã¿´Î²ÉÑùµÄÒôƵÐÅÏ¢ÓÃ2¸ö×Ö½Ú±£´æ¡£¿ÉÒÔ¶Ô±ÈÏÂbmpÎļþÓ÷ֱðÓÃ2¸ö×Ö½Ú±£´æRGBÑÕÉ«µÄÐÅÏ¢¡£ 16000²ÉÑùÂÊ ÊÇÖ¸ 1ÃëÖÓ²ÉÑù 16000´Î¡£³£¼ûµÄÒôƵÊÇ44100HZ£¬¼´Ò»Ãë²ÉÑù44100´Î¡£ µ¥ÉùµÀ£º Ö»ÓÐÒ»¸öÉùµÀ¡£
     *
     * ¸ù¾ÝÕâЩÐÅÏ¢£¬ÎÒÃÇ¿ÉÒÔ¼ÆË㣺 1ÃëµÄ16000²ÉÑùÂÊÒôƵÎļþ´óСÊÇ 2*16000 = 32000×Ö½Ú £¬Ô¼Îª32K 1ÃëµÄ8000²ÉÑùÂÊÒôƵÎļþ´óСÊÇ 2*8000 = 16000×Ö½Ú £¬Ô¼Îª 16K
     *
     * Èç¹ûÒÑ֪¼Òôʱ³¤£¬¿ÉÒÔ¸ù¾ÝÎļþµÄ´óС¼ÆËã²ÉÑùÂÊÊÇ·ñÕý³£¡£
     * @param filePath
     * @return
     */
    public static long getPCMDurationMilliSecond(String filePath) {
        File file = new File(filePath);
 
        //µÃµ½¶àÉÙÃë
        long second = file.length() / 32000 ;
 
        long milliSecond = Math.round((file.length() % 32000)   / 32000.0  * 1000 ) ;
 
        return second * 1000 + milliSecond;
    }
}