資源簡介
java sound 簡單播放器,這是我自學(xué)Java Sound后編寫的一個簡單播放程序,另外我還上傳了一個基于Java Sound的錄音程序。

代碼片段和文件信息
package?com.xzz.applet.service;
import?java.io.BufferedInputStream;
import?java.io.IOException;
import?java.net.Socket;
import?javax.sound.sampled.AudioFormat;
import?javax.sound.sampled.AudioSystem;
import?javax.sound.sampled.DataLine;
import?javax.sound.sampled.LineUnavailableException;
import?javax.sound.sampled.SourceDataLine;
public?class?SimpleAudioPlayer?{
public?SourceDataLine?sdLine;
public?AudioFormat?audioFormat;
private?PlayThread?playThread;
private?Socket?soc;
public?boolean?stop?=?true;
public?SimpleAudioPlayer(Socket?soc)?throws?LineUnavailableException?{
audioFormat?=?MyAudioFormat.getInstance();
DataLine.Info?dataLineInfo?=?new?DataLine.Info(SourceDataLine.class
audioFormat);
sdLine?=?(SourceDataLine)?AudioSystem.getLine(dataLineInfo);
sdLine.open();
this.soc?=?soc;
}
public?void?start()?throws?IllegalRecordStateException?{
if?(stop)?{
sdLine.start();
playThread?=?new?PlayThread();
playThread.start();
}?else?{
throw?new?IllegalRecordStateException(
“Illegal?playing?state!?Player?is?active?at?the?time.“);
}
}
public?void?stop()?{
stop?=?true;
playThread.stopService();
playThread?=?null;
sdLine.stop();
}
public?void?close(){
sdLine.close();
}
class?PlayThread?extends?Thread?{
private?BufferedInputStream?in;
@Override
public?void?run()?{
try?{
in?=?new?BufferedInputStream(soc.getInputStream());
}?catch?(IOException?e1)?{
e1.printStackTrace();
}
byte[]?buf?=?new?byte[1024?*?1];
int?readed?=?-1;
try?{
while?(stop)?{
if?((readed?=?in.read(buf))?>?0)?{
sdLine.write(buf?0?readed);
}?else
break;
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
public?void?stopService(){
try?{
in.close();
}?catch?(Exception?e)?{
}
}
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1931??2011-02-19?00:18??SimpleAudioPla
-----------?---------??----------?-----??----
?????????????????1931????????????????????1
評論
共有 條評論