資源簡介
自己寫的java音頻播放實例

代碼片段和文件信息
package?com.test;
import?javax.sound.sampled.AudioInputStream;
import?javax.sound.sampled.AudioSystem;
import?javax.sound.sampled.AudioFormat;
import?javax.sound.sampled.DataLine;
import?javax.sound.sampled.UnsupportedAudioFileException;
import?javax.sound.sampled.SourceDataLine;
import?java.util.Date;
import?java.io.IOException;
import?java.io.File;
public?class?BasicPlayer?implements?Runnable{
//音頻輸入流
private?AudioInputStream?stream?=?null;
//音頻格式
private?AudioFormat?format?=?null;
//源數據行
private?SourceDataLine?sourceDataLine;
//緩沖區大小
private?static?final?int?BUFFER_SIZE=1024*50;
private?File?fileName;
//能否播放
public?static?boolean?isActive=true;
//是否停止
public?static?boolean?isStop=true;
/**
?*?構造器
?*?@param?fileName?音頻文件
?*/
public??BasicPlayer(File?fileName)?{
this.fileName=fileName;
}
/**
?*?播放音頻文件
?*/
public?void?play(){
try?{
//從提供的?File?獲得音頻輸入流
stream=AudioSystem.getAudioInputStream(fileName);
//獲得此音頻輸入流中聲音數據的音頻格式
format=stream.getFormat();
//音頻編碼轉換
if?(format.getEncoding()?!=?AudioFormat.Encoding.PCM_SIGNED)?{
format?=?new?AudioFormat(
AudioFormat.Encoding.PCM_SIGNED????//音頻編碼技術
format.getSampleRate() ? //每秒的樣本數
16 //每個樣本的位數
format.getChannels() //聲道數
format.getChannels()?*?2 //每幀的字節數
format.getSampleRate() //每秒的幀數
false //指示是否以big-endian字節順序存儲單個樣本中的數據(false?意味著?little-endian)
);
//格式化音頻輸入流
stream?=?AudioSystem.getAudioInputStream(format?stream);
}
//獲得源數據行
sourceDataLine=getDataLine(format);
//允許數據行執行數據?I/O操作
sourceDataLine.start();
int?inBytes=0;
byte[]?audioData=new?byte[BUFFER_SIZE];
BasicPlayer.isStop=false;
Date?date=new?Date();
System.out.println(“開始時間:“+date.getMinutes()+“:“+date.getSeconds());
while(inBytes!=-1){
//判斷能否播放
if(BasicPlayer.isActive){
try?{
Thread.sleep(10);
}?catch?(InterruptedException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
// System.out.println(“處于播放狀態“);
//從音頻輸入流讀取一定數量的字節,并將其存儲在緩沖區數組audioData中
inBytes?=?stream.read(audioData?0?BUFFER_SIZE);
if?(inBytes?>=?0)?{
//通過此源數據行將音頻數據寫入混頻器
int?outBytes?=?sourceDataLine.write(audioData?0?inBytes);
}
}else{
try?{
Thread.sleep(1000);
}?catch?(InterruptedException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
// System.out.println(“處于暫停狀態“);
}
}
date=new?Date();
System.out.println(“結束時間:“+date.getMinutes()+“:“+date.getSeconds());
BasicPlayer.isStop=true;
sourceDataLine.drain();
sourceDataLine.stop();
}?catch?(IOException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(UnsupportedAudioFileException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}finall
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄??????????0??2008-01-22?15:16??javaSound
?????文件???????4309??2008-01-22?15:13??javaSound\BasicPla
?????文件???????1120??2008-01-22?14:59??javaSound\Pla
?????文件????????848??2008-01-22?15:02??javaSound\Test.java
-----------?---------??----------?-----??----
?????????????????6277????????????????????4
- 上一篇:ffmpeg-3.1.2-1.2.jar
- 下一篇:java 實現旋轉的八卦
評論
共有 條評論