java中如何获取音频(mp3)长度 请教java如何实现获取一段mp3的总时长
java \u600e\u4e48\u83b7\u53d6\u97f3\u9891\u6587\u4ef6\u65f6\u957fjava\u4e2d\u76f4\u63a5\u4f7f\u7528AudioInputStream\u7c7b\u6765\u64cd\u4f5c\u97f3\u4e50\u6587\u4ef6\uff0c\u83b7\u53d6\u65f6\u957f\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class AudioLength {
public static void main(String[] args) throws LineUnavailableException,
UnsupportedAudioFileException, IOException {
File file = new File("d:/test.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
clip.open(ais);
System.out.println( clip.getMicrosecondLength() / 1000000D + " s" );//\u83b7\u53d6\u97f3\u9891\u6587\u4ef6\u65f6\u957f
}
}
\u5728\u5bfc\u5165\u4e00\u4e2ajava-1.0.2\u5305 \u5c31\u53ef\u4ee5\u4e86 \u5982\u679c\u662f\u83b7\u53d6\u7f51\u7edc\u6587\u4ef6\uff0c\u90a3\u5c31\u9700\u8981\u5148\u628a\u6587\u4ef6\u4e0b\u8f7d\u5230\u672c\u5730\uff0c\u7136\u540e\u518d\u53bb\u83b7\u53d6\u97f3\u9891\u65f6\u957f
2.压缩格式。
基本上就跟这俩东西有关了。
有了这个以后,可以根据文件的大小来计算播放时长。
至于具体的,我也没算过。我觉得你可以cut一段音乐,比如wma格式的,看看一分钟是多大。然后用你的文件大小来和这个标准文件作比较。
还有其他方法就是借助外部包jar,也可使用JMF框架。
找个第三方的,MP3播放器的jar包,就能取出其长度
绛旓細濯掍綋鎾斁鍣鐨鏍稿績鍔熻兘閫氳繃JMF锛圝ava Media Framework锛堿PI瀹炵幇銆侸MF鎵╁睍浜咼2SE骞冲彴鐨勫濯掍綋鑳藉姏锛屽厑璁窲ava搴旂敤鍜孉pplet鎴彇銆佸洖鏀俱佽浆鎹㈠寘鎷闊抽鍜岃棰戝湪鍐呯殑澶氱濯掍綋銆侸MF鏀寔澶氱濯掍綋鏍煎紡锛屽叿浣撹鍙傝Supported Media Formats and Capture Devices銆侫IFF (.aiff)AVI (.avi)GSM (.gsm)HotMedia (.mvr)MID...
绛旓細2015-08-06 鏈浣崇瓟妗 涓鑸笉瑁呮彃浠舵儏鍐典笅,jdk鑷甫绫诲簱鍙兘瀹炵幇AU,AIFF,WAV,MIDI,RFM绛夋牸寮鐨勯煶棰,鍔犱簡鎻掍欢鍚庢墠鑳藉疄鐜mp3绛夈 鏈洖绛旂敱鐢佃剳缃戠粶鍒嗙被杈句汉 钁h緣鎺ㄨ崘 涓炬姤| 绛旀绾犻敊 | 璇勮 11 1 鏇归槼闃砪yy 閲囩撼鐜:63% 鏉ヨ嚜鍥㈤槦:鏄熶箣娴╁彲 鎿呴暱: 姹傝亴灏变笟 ...
绛旓細package mp3;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;public class ReadMp3 { private SongInfo info = null;private RandomAccessFile ran = null;private File file = null;public ReadMp3() throws FileNotFound...
绛旓細import java.io.FileInputStream;import javax.swing.JApplet;import sun.audio.AudioPlayer;import sun.audio.AudioStream;public class Sound1 extends JApplet { / Create the applet./ public static void main(String args[]){ try { FileInputStream fileau=new FileInputStream("灏.mp3");Au...
绛旓細//姹楋紝绠$悊鍛樸傝繖鐪鐨鏄垜鑷繁鍦‥clipse涓婂啓濂斤紝鐒跺悗涓閿鍒朵笂鏉ョ殑锛屼笉鏄嫹璐濆埆浜虹殑銆JAVA浠g爜閮介暱杩欐牱濂藉悧...import java.io.File;import java.io.IOException;public class text {//鏋氫妇涓涓枃浠跺す涓嬬殑鎵鏈MP3鏂囦欢锛堜笉鍖呮嫭瀛愮洰褰曪級public static void main(String[] args) {System.out.println(...
绛旓細涓嶅ソ鎰忔濓紝java璇█娌℃湁鎻愪緵瀵MP3澹伴煶鏂囦欢鐨鍐呭湪鏀寔銆備笉杩囦篃涓嶇敤閬楁喚锛屼綔涓哄讥琛,Sun宸茬粡鎺ㄥ嚭浜嗕笓闂ㄧ敤浜庢挱鏀捐繖绉嶅濯掍綋闊抽鎴栬棰戠殑鏂囦欢鐨勫寘鈥斺攋mf锛圝ava濯掍綋妗嗘灦锛夊寘锛岃鍒癝un鐨勭綉绔欙紙http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html锛夊幓涓嬭浇杩欎釜鍖呫傜敤娉曚妇渚嬪涓嬶細File ...
绛旓細mp3NO++;System.out.println(mp3NO);if(mp3NO<mp3List.size()){ this.start();} return;} //褰鎻愬彇濯掍綋鐨鍐呭缁撴潫 if (e instanceof PrefetchCompleteEvent) { System.out.println("鍐呭缁撴潫");player.start();return;} //褰撳疄渚嬪寲鍚 if (e instanceof RealizeCompleteEvent) { System.out....
绛旓細绠鍗鐨瀹炰緥锛屼唬鐮佸涓嬶紝绾补JMF鍔犺浇MP3骞舵挱鏀撅細import javax.media.*;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class PlayerMusic implements ControllerListener {// ControllerListener // 鎺у埗浜嬩欢 private Player player;private boolean first, loop;private ...
绛旓細//:URLReader.java === import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.net.URL;public class URLReader { public static void readAndSave(String url,String fileFullName)throws Exception{ System.out.println("Starting.");URL...
绛旓細Java鍙互鎾斁wav鐨勫0闊锛屼笉鐢ㄧ壒鎰忓幓鏀MP3鍚э紝浣犺浆鎹㈡垚wav鏍煎紡 鎾斁鎵撲唬鐮佸涓 File file1 = new File("src/music/22.wav");//file1= new File(this.getClass().getResource(path).getPath());AudioClip chosenClip ;chosenClip = Applet.newAudioClip(file1.toURL());chosenClip.loop();...