怎样在Java图形界面添加背景音乐
展开全部
Java中可以通过AudioClip类来实现音乐播放,循环等操作。AudioClip支持的音晌橘乐格式有.wav、.mid、AIFF、AU、RMF,但是格式要求相当严格。我用AudioClip播放我自己录的一段wav文件就没有声音,让我纠结了很久,最后才发现我的wav文件内容没有写文件尾,对于格式要求严格的AudioClip而言是无法识别的(这个问题困扰了我整晚)。
AudioCLip主要的方法有:play()播放依次声音;loop()循环播放音乐;stop()停止播放。
做法一:
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();
AudioCLip主要的方法有:play()播放依次声音;loop()循环播放音乐;stop()停止播放。
做法一:
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();
展开全部
import java.applet.*;
import java.awt.*;
..............
//创建图形界面
AudioClip clip=Applet.newAudioClip(路径URL);
clip.play(); //播放
/*
clip.stop(); 停止
clip.loop(); 循环播放
*/
还敏茄缓可以纳高用桥模javax.sound.sampled包
import java.awt.*;
..............
//创建图形界面
AudioClip clip=Applet.newAudioClip(路径URL);
clip.play(); //播放
/*
clip.stop(); 停止
clip.loop(); 循环播放
*/
还敏茄缓可以纳高用桥模javax.sound.sampled包
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.IOException;
import java.net.URL;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
class MidiSequence {
private Sequencer sequencer;
//庆启provide Sequence as a read-only property
private Sequence song;
public Sequence getSong() { return song; }
//filename property is read-only
private String filename;
public String getFilename() { return filename; }
//looping property
private boolean looping = false;
public boolean getLooping() { return looping; }
public void setLooping(boolean _looping) { looping = _looping; }
//巧差携repeat property
private int repeat = 0;
public void setRepeat(int _repeat) { repeat = _repeat; }
public int getRepeat() { return repeat; }
//returns whether the sequence is ready for action
public boolean isLoaded() {
return (boolean)(sequencer.isOpen());
}
//primary constructor
public MidiSequence() {
try {
//fire up the sequencer
sequencer = MidiSystem.getSequencer();
} catch (MidiUnavailableException e) { }
}
//overloaded constructor accepts a filename
public MidiSequence(String filename) {
/孝伏/call default constructor
this();
//load the midi file
load(filename);
}
private URL getURL(String filename) {
URL url = null;
try {
url = this.getClass().getResource(filename);
}
catch (Exception e) { }
return url;
}
//load a midi file into a sequence
public boolean load(String midifile) {
try {
//load the midi file into the sequencer
filename = midifile;
song = MidiSystem.getSequence(getURL(filename));
sequencer.setSequence(song);
sequencer.open();
return true;
} catch (InvalidMidiDataException e) {
return false;
} catch (MidiUnavailableException e) {
return false;
} catch (IOException e) {
return false;
}
}
//play the midi sequence
public void play() {
if (!sequencer.isOpen()) return;
if (looping) {
sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
sequencer.start();
} else {
sequencer.setLoopCount(repeat);
sequencer.start();
}
}
//stop the midi sequence
public void stop() {
sequencer.stop();
}
}
public class myFrame extends JFrame{
public static void main(String []args){
this.setSize(500,500);
this.setTitle("my frame");
this.setVisible(true);
MidiSequence se=new MidiSequence ("music.wav");
se.play();
setLooping(true);
try{
Thread.sleep(10000);
}catch(Exception e){
}
se.stop();
}
}
import java.net.URL;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
class MidiSequence {
private Sequencer sequencer;
//庆启provide Sequence as a read-only property
private Sequence song;
public Sequence getSong() { return song; }
//filename property is read-only
private String filename;
public String getFilename() { return filename; }
//looping property
private boolean looping = false;
public boolean getLooping() { return looping; }
public void setLooping(boolean _looping) { looping = _looping; }
//巧差携repeat property
private int repeat = 0;
public void setRepeat(int _repeat) { repeat = _repeat; }
public int getRepeat() { return repeat; }
//returns whether the sequence is ready for action
public boolean isLoaded() {
return (boolean)(sequencer.isOpen());
}
//primary constructor
public MidiSequence() {
try {
//fire up the sequencer
sequencer = MidiSystem.getSequencer();
} catch (MidiUnavailableException e) { }
}
//overloaded constructor accepts a filename
public MidiSequence(String filename) {
/孝伏/call default constructor
this();
//load the midi file
load(filename);
}
private URL getURL(String filename) {
URL url = null;
try {
url = this.getClass().getResource(filename);
}
catch (Exception e) { }
return url;
}
//load a midi file into a sequence
public boolean load(String midifile) {
try {
//load the midi file into the sequencer
filename = midifile;
song = MidiSystem.getSequence(getURL(filename));
sequencer.setSequence(song);
sequencer.open();
return true;
} catch (InvalidMidiDataException e) {
return false;
} catch (MidiUnavailableException e) {
return false;
} catch (IOException e) {
return false;
}
}
//play the midi sequence
public void play() {
if (!sequencer.isOpen()) return;
if (looping) {
sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
sequencer.start();
} else {
sequencer.setLoopCount(repeat);
sequencer.start();
}
}
//stop the midi sequence
public void stop() {
sequencer.stop();
}
}
public class myFrame extends JFrame{
public static void main(String []args){
this.setSize(500,500);
this.setTitle("my frame");
this.setVisible(true);
MidiSequence se=new MidiSequence ("music.wav");
se.play();
setLooping(true);
try{
Thread.sleep(10000);
}catch(Exception e){
}
se.stop();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
陪楼主等答案
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询