java中我想实现用按钮来控制音乐播放与停止 下面是我的部分代码 但实现不了播放和停止 谁能帮我解决一下
publicclassChessMusicextendsThread{publicChessMusic(){}privateStringfilename;publicCh...
public class ChessMusic extends Thread {
public ChessMusic () {
}
private String filename;
public ChessMusic (String wavfile) {
this.filename = wavfile;
}
public void run() {
while(true){
File soundFile=null;
soundFile = new File(filename);
AudioInputStream audioInputStream = null;
try {
//获得音乐流
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) 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();
}
}
}
} 展开
public ChessMusic () {
}
private String filename;
public ChessMusic (String wavfile) {
this.filename = wavfile;
}
public void run() {
while(true){
File soundFile=null;
soundFile = new File(filename);
AudioInputStream audioInputStream = null;
try {
//获得音乐流
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) 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();
}
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询