不知道你是在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目錄