我想把我的java小程序中嵌入一个背景音乐,写出添加背景音乐代码 。放音乐的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目录
怎么在Java程序中加音乐?可以通过Service来播放背景音乐动画加音乐java源代码,以下是实现代码:
1.在AndroidManifest.xml文件中动画加音乐java源代码的application标签内加入下边语句
service android:name=".MusicServer"
intent-filter
action android:name="com.angel.Android.MUSIC"/
category android:name="android.intent.category.default" /
/intent-filter
/service
2.新建MusicServer.java类,内容为
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicServer extends Service {
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent,int startId){
推荐阅读
- 班组游戏动作要领,班组游戏活动方案
- sqlserver2008安装错误日志,sql server安装出错
- 电脑密码怎么绕开,电脑密码怎么绕过
- 钉钉直播怎么发成链接,钉钉直播怎么发送链接
- mysql签到表怎么设置 mysql 标签表设计
- 小儿推拿如何推广,小儿推拿的推广话术
- pc竞速游戏推荐,pc竞速游戏推荐手游
- sap显示分割分配清单,sap分配分摊
- php接收手机端数据 php接收post