package com;
|
|
import java.io.IOException;
|
import java.util.Vector;
|
|
import javax.sound.sampled.AudioFormat;
|
import javax.sound.sampled.SourceDataLine;
|
|
import main.main_window;
|
|
//²¥·ÅÉùÒôµÄÀà
|
public class PlaySound extends Thread {
|
Vector<Vector<Object>> vvo = null;
|
public boolean is_running = true;
|
private boolean sound_en = true;
|
//private boolean voice_en;
|
private String filename;
|
//private String al_text;
|
|
public PlaySound(String wav_file) {
|
filename = "/sound/" + wav_file;
|
this.start();
|
}
|
|
public void run()
|
{
|
/**********************************************************************/
|
if(true == sound_en) {
|
javax.sound.sampled.AudioInputStream audioInputStream = null;
|
try {
|
audioInputStream = javax.sound.sampled.AudioSystem.getAudioInputStream(main_window.class.getResource(filename));
|
} catch (Exception e1) {
|
e1.printStackTrace();
|
return;
|
}
|
|
AudioFormat format = audioInputStream.getFormat();
|
SourceDataLine auline = null;
|
javax.sound.sampled.DataLine.Info info = new javax.sound.sampled.DataLine.Info(SourceDataLine.class, format);
|
try {
|
auline = (SourceDataLine) javax.sound.sampled.AudioSystem.getLine(info);
|
auline.open(format);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return;
|
}
|
|
auline.start();
|
int nBytesRead = 0;
|
byte[] abData = new byte[512];
|
try {
|
while (nBytesRead != -1) {
|
nBytesRead = audioInputStream.read(abData, 0, abData.length);
|
if (nBytesRead >= 0)
|
auline.write(abData, 0, nBytesRead);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
return;
|
} finally {
|
auline.drain();
|
auline.close();
|
}
|
}
|
/**********************************************************************/
|
//while(vvo.size() > 0)
|
{/*
|
Vector<Object> vo = vvo.get(0);
|
sound_en = (boolean) vo.get(0);
|
voice_en = (boolean) vo.get(1);
|
filename = "/sound/" + vo.get(2);
|
al_text = (String) vo.get(3);
|
|
if(true == sound_en) {
|
javax.sound.sampled.AudioInputStream audioInputStream = null;
|
try {
|
audioInputStream = javax.sound.sampled.AudioSystem.getAudioInputStream(main_window.class.getResource(filename));
|
} catch (Exception e1) {
|
e1.printStackTrace();
|
return;
|
}
|
|
AudioFormat format = audioInputStream.getFormat();
|
SourceDataLine auline = null;
|
javax.sound.sampled.DataLine.Info info = new javax.sound.sampled.DataLine.Info(SourceDataLine.class, format);
|
try {
|
auline = (SourceDataLine) javax.sound.sampled.AudioSystem.getLine(info);
|
auline.open(format);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return;
|
}
|
|
auline.start();
|
int nBytesRead = 0;
|
byte[] abData = new byte[512];
|
try {
|
while (nBytesRead != -1) {
|
nBytesRead = audioInputStream.read(abData, 0, abData.length);
|
if (nBytesRead >= 0)
|
auline.write(abData, 0, nBytesRead);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
return;
|
} finally {
|
auline.drain();
|
auline.close();
|
}
|
}*/
|
/**********************************************************************/
|
/*
|
if(true == voice_en) {
|
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
|
Dispatch sapo = (Dispatch) sap.getObject();
|
try {
|
// ÒôÁ¿ 0-100
|
sap.setProperty("Volume", new Variant(100));
|
// ÓïÒôÀʶÁËÙ¶È -10 µ½ +10
|
sap.setProperty("Rate", new Variant(-5));
|
Dispatch.call(sapo, "Speak", new Variant(al_text));
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
sapo.safeRelease();
|
sap.safeRelease();
|
}
|
}*/
|
/**********************************************************************/
|
//vvo.removeElementAt(0);
|
/**********************************************************************/
|
}
|
/***********************************************************************************************/
|
is_running = false;
|
/***********************************************************************************************/
|
}
|
}
|
|
|