在eclipse中怎样插入音乐,不是插入播放器,而是插入单独的音乐。 5
小弟今年作业做的是java编仿windows扫雷,想在游戏结束后(扫雷失败后)加上一点音乐,请问应该怎么办啊?请给我具体代码或含有代码的链接,谢谢了先。我的邮箱:3509...
小弟今年作业做的是java编仿windows扫雷,想在游戏结束后(扫雷失败后)加上一点音乐,请问应该怎么办啊?请给我具体代码或含有代码的链接,谢谢了先。
我的邮箱:350999506@qq.com
可以把代码发我邮箱,不胜感激。 展开
我的邮箱:350999506@qq.com
可以把代码发我邮箱,不胜感激。 展开
1个回答
展开全部
import java.io.*;
import javax.sound.sampled.*;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//修改你的音乐文件路径就OK了
AePlayWave apw=new AePlayWave("./111.wav");
apw.start();
}
}
//在程序中实例化这个类,启动线程,实例化的时候参照Test修改路径就OK
//播放声音的类
class AePlayWave extends Thread {
private String filename;
public AePlayWave(String wavfile) {
filename = wavfile;
}
public void run() {
File 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();
}
}
}
有不明白的地方再hi我
import javax.sound.sampled.*;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//修改你的音乐文件路径就OK了
AePlayWave apw=new AePlayWave("./111.wav");
apw.start();
}
}
//在程序中实例化这个类,启动线程,实例化的时候参照Test修改路径就OK
//播放声音的类
class AePlayWave extends Thread {
private String filename;
public AePlayWave(String wavfile) {
filename = wavfile;
}
public void run() {
File 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();
}
}
}
有不明白的地方再hi我
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询