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;
|
}
|
}
|