資源簡介
自己寫的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;
//源數(shù)據(jù)行
private?SourceDataLine?sourceDataLine;
//緩沖區(qū)大小
private?static?final?int?BUFFER_SIZE=1024*50;
private?File?fileName;
//能否播放
public?static?boolean?isActive=true;
//是否停止
public?static?boolean?isStop=true;
/**
?*?構(gòu)造器
?*?@param?fileName?音頻文件
?*/
public??BasicPlayer(File?fileName)?{
this.fileName=fileName;
}
/**
?*?播放音頻文件
?*/
public?void?play(){
try?{
//從提供的?File?獲得音頻輸入流
stream=AudioSystem.getAudioInputStream(fileName);
//獲得此音頻輸入流中聲音數(shù)據(jù)的音頻格式
format=stream.getFormat();
//音頻編碼轉(zhuǎn)換
if?(format.getEncoding()?!=?AudioFormat.Encoding.PCM_SIGNED)?{
format?=?new?AudioFormat(
AudioFormat.Encoding.PCM_SIGNED????//音頻編碼技術(shù)
format.getSampleRate() ? //每秒的樣本數(shù)
16 //每個樣本的位數(shù)
format.getChannels() //聲道數(shù)
format.getChannels()?*?2 //每幀的字節(jié)數(shù)
format.getSampleRate() //每秒的幀數(shù)
false //指示是否以big-endian字節(jié)順序存儲單個樣本中的數(shù)據(jù)(false?意味著?little-endian)
);
//格式化音頻輸入流
stream?=?AudioSystem.getAudioInputStream(format?stream);
}
//獲得源數(shù)據(jù)行
sourceDataLine=getDataLine(format);
//允許數(shù)據(jù)行執(zhí)行數(shù)據(jù)?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(“處于播放狀態(tài)“);
//從音頻輸入流讀取一定數(shù)量的字節(jié),并將其存儲在緩沖區(qū)數(shù)組audioData中
inBytes?=?stream.read(audioData?0?BUFFER_SIZE);
if?(inBytes?>=?0)?{
//通過此源數(shù)據(jù)行將音頻數(shù)據(jù)寫入混頻器
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(“處于暫停狀態(tài)“);
}
}
date=new?Date();
System.out.println(“結(jié)束時間:“+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
評論
共有 條評論