放音乐的api网上查有很多,比如javax.sound.midi.*;
支持midi,mid背景音乐的播放
public class Music implements MetaEventListener, Runnable{
private Sequence sequence = null;
private Sequencer sequencer;
private boolean isPlaying = false;
private volatile Thread thread;
public Music(){
}
public Music(String midifile){
try {
loadMidi(midifile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidMidiDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//导入midi文件到内存中传给Sequence对象,相当与编码器
public void loadMidi(String filename) throws IOException, InvalidMidiDataException{
sequence = MidiSystem.getSequence(this.getClass().getResourceAsStream(filename));
}
//播放方法
public void play(){
if(isPlaying){
return;
}
try {
sequencer = MidiSystem.getSequencer();
sequencer.open();
//用Sequencer对象把声音文件序列解码出来播放
sequencer.setSequence(sequence);
sequencer.addMetaEventListener(this);
//设置循环次数,-1表示一直循环
sequencer.setLoopCount(-1);
sequencer.setLoopStartPoint(0);
sequencer.setLoopEndPoint(sequencer.getTickLength());
} catch (MidiUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidMidiDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(thread == null){
thread = new Thread(this);
thread.start();
}
}
public void stop(){
if(isPlaying){
sequencer.stop();
isPlaying = false;
}
if(thread != null){
thread = null;
}
}
public void meta(MetaMessage meta) {
if(meta.getType() == 47){
System.out.println("Sequencer is done playing");
}
// TODO Auto-generated method stub
}
public void run() {
// TODO Auto-generated method stub
Thread current = Thread.currentThread();
while(current == thread && !isPlaying){
sequencer.start();
isPlaying = true;
try {
thread.sleep(1001);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//用起来也很方便
public static void main(String[] args){
Music music = new Music("a.mid");
music.play();
}
}
如果这样写路径类要和音频文件放在一个目录下,如果你不想这样,有两种方法,一种是修改路径字符串,另一种是把Class.getResourceAsStream方法改成new FileInputStream 这两个方法加载资源的初始路径不同,前者找class文件所在目录,后者找project目录
我感觉应该是浏览器的问题,Windows和linux的浏览器不一样,现实的效果也不一样,linux的火狐浏览器跟Windows的IE浏览器有很大的区别,IE跟好多浏览器都不兼容。万恶的IE……
你确定你的linux嵌入式开发板的系统里面,有你所用的java翻译模块吗(这是软件),更新一下linux里的java数据库。
这种情况下应该重新发布/部署之后才能跟中间件同步吧?