不知道你是在java里哪添加?Swing界面中吗?
下面这个是我之前做Swing界面程序时添加音乐的代码,专希望对你有帮助属
AudioClip[] musics;//定义音乐集合
musics = new AudioClip[2];//初始化
URL url1 = this.getClass().getResource("/ReadyGo.WAV"); //定义音乐文件地址
URL url2 = this.getClass().getResource("/back1.mid"); //定义音乐文件地址
musics[0] = JApplet.newAudioClip(url1);
musics[1] = JApplet.newAudioClip(url2);
musics[0].play();//音乐开始执行
musics[1].stop();//停止播放
B. java中Swing怎么添加音乐
JAVA应用程序添加背景音乐示例:
1.先定义一个 URL 对象,并赋NULL值;
URL musicURL = null;
2.然后为定义的 URL 赋值,即得到要播放的URL地址,
musicURL = new URL( "音乐地址");//得到要播放音乐的url
3.创建一个播放音频的实例,并得到这个实例
AudioClip ac = Applet.newAudioClip(musicURL); //得到一个播放音频的实例
4.进行播放控制
ac.play(); //播放一编
ac.loop(); //循环播放
ac.stop();//停止
播放本地音乐示例:
private void playMusic(){
try {
javax.media.Player player;
if(null==player){
File musicFile=new File("./music.mp3");//得到一个MP3文件
if(musicFile.exists()){
MediaLocator locator=new MediaLocator("file:"+musicFile.getAbsolutePath());
player = Manager.createRealizedPlayer(locator);
player.prefetch();//预读文件
}else{
System.err.println(pro.getMusicFile()+" 找不到");
}
}
//centerpanel.add(player.getControlPanelComponent()!=null?player.getControlPanelComponent():null);//显示播放工具
player.start();//播放
} catch (CannotRealizeException ex) {
ex.printStackTrace();
} catch (NoPlayerException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
C. Eclipse中Java图形界面添加背景音乐
做法一:
InputStream is =null;
AudioStream as = null ;
is = getClass().getResourceAsStream("a.wav");
try {
as = new AudioStream(is);
} catch (IOException e) {}
AudioPlayer.player.start(as);
此方法将音乐文件放入流中在播放,仅限于Java Application,容易报错,空指针异常,或者是流异常,不推荐。
做法二:
String music = "a.wav";
AudioClip clip = Applet.newAudioClip(getClass().getResource(music));
次方法在Applet中运行没有问题,但是Application中getclass()会返回空指针,导致失败。
推荐做法:
private URL url;
private AudioClip ac;
File f1 = new File("C:/3.wav");
try {
url= f1.toURL();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ac= Applet.newAudioClip(cb1);
ac.play();
D. 我用JAVA写好了一个窗体游戏,怎么加背景音乐
//有些的代码,如下:
packagecom.music;
importjava.applet.AudioClip;
importjava.io.*;
importjava.applet.Applet;
importjava.awt.Frame;
importjava.net.MalformedURLException;
importjava.net.URI;
importjava.net.URL;
importjavax.swing.JFrame;
publicclassMusicextendsJFrame{
Filef;
URIuri;
URLurl;
Music(){
try{
f=newFile("TaylorSwift-SparksFly.wav");
uri=f.toURI();
url=uri.toURL();//解析地址
AudioClipaau;
aau=Applet.newAudioClip(url);
aau.loop();//循环播放
}
catch(Exceptione)
{
e.printStackTrace();
}
}
publicstaticvoidmain(Stringargs[]){
newMusic();
}
}
E. Java程序怎样添加背景音乐
你可以试一下下面的代码
import java.applet.AudioClip;
import java.io.*;
import java.applet.Applet;
import java.awt.Frame;
import java.net.MalformedURLException;
import java.net.URL;
public class JavaClip extends Frame{
public JavaClip(){
super();
}
public static void main(String args[]) {
try {
URL cb;
File f = new File("C:\\Documents and Settings\\Administrator\\桌面\\刀出鞘.wav"); //引号里面的是音乐文件所在的绝对鹿筋
cb = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(cb);
//aau.play();
aau.loop();
//循环播放 aau.play() 单曲 aau.stop()停止播放
JavaClip frame=new JavaClip();
//frame.setBounds(0, 0, 300, 200);
//frame.setVisible(true);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
F. Java 中如何插入背景音乐
import java.awt.BorderLayout;
import java.awt.Component;
import java.net.URL;
import javax.media.*;
public class PlayerApplet extends JApplet implements ControllerListener{
Player player = null;
public void init(){
setLayout(new BorderLayout());
String mediaFile = getParameter("FILE");
try{
URL mediaURL = new URL(getDocumentBase(),mediaFile);
player = Manager.createPlayer(mediaURL);
player.addControllerListener(this);
}catch(Exception ex){
System.err.println(ex.getMessage());
}
}
public void start(){
player.prefetch();
}
public void stop(){
//player.stop();
//player.deallocate();
}
public void destory(){
player.close();
}
public synchronized void controllerUpdate(ControllerEvent event){
if( event instanceof RealizeCompleteEvent ){
Component comp;
if(( comp = player.getVisualComponent() ) != null )
add("Center",comp);
if(( comp = player.getControlPanelComponent() ) != null)
add("South",comp);
validate();
}else if( event instanceof PrefetchCompleteEvent ){
player.setRate(3.0F);
player.start();
}
}
}
G. 我想在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目录