?? sound.java
字號(hào):
package vo.sounds;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sound {
String[] sounds = { "mis/PUTCHESS.WAV", "mis/WIN.WAV", "mis/LOSS.WAV" };
private AudioInputStream ais;
private SourceDataLine line;
private AudioFormat baseFormat;
private static final int BUFFER_SIZE = 4000 * 4;
/** 用于實(shí)現(xiàn)單子模式 */
private static Sound soundTem = null;
/**
* 落子的聲音
*/
public static int PUTCHESS = 0;
/**
* 表示贏
*/
public static int WIN = 1;
/**
* 輸?shù)粲螒虻穆曇? */
public static int LOSS = 2;
private Sound() {
super();
}
public static Sound getSound() {
if(soundTem == null) {
soundTem = new Sound();
return soundTem;
} else {
return soundTem;
}
}
private SourceDataLine getLine(AudioFormat audioFormat) {
SourceDataLine res = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat);
} catch (Exception e) {
}
return res;
}
public void play(int sound) {
try {
ais = AudioSystem.getAudioInputStream(new File(sounds[sound]));
baseFormat = ais.getFormat();
line = getLine(baseFormat);
line.start();
int inBytes = 0;
byte[] audioData = new byte[BUFFER_SIZE];
while (inBytes != -1) {
inBytes = ais.read(audioData, 0, BUFFER_SIZE);
if (inBytes >= 0) {
line.write(audioData, 0, inBytes);
}
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
} catch (UnsupportedAudioFileException ex) {
System.out.println(ex.getMessage());
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -