如何在Java小程序中添加音乐
展开全部
AudioClip audio1=getAudioClip(codebase,"danny.au");可以写在paint函数中,也可以写在别的函数中。Java applet除了可以播放.au的音乐文件还可以播放.wav的音乐文件,要播放mp3文件需要有JMF插件。
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Graphics;
import java.net.URL;
public class PlaySound extends Applet
{
public void paint(Graphics g)
{
URL codebase=getCodeBase();
System.out.println(codebase);
AudioClip audio1=getAudioClip(codebase,"danny.au");
g.drawString("Play music",30,30);
audio1.loop();
}
}
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Graphics;
import java.net.URL;
public class PlaySound extends Applet
{
public void paint(Graphics g)
{
URL codebase=getCodeBase();
System.out.println(codebase);
AudioClip audio1=getAudioClip(codebase,"danny.au");
g.drawString("Play music",30,30);
audio1.loop();
}
}
展开全部
播放音乐,需要一个音乐驱动
package Game;
import java.io.InputStream;
import javazoom.jl.player.advanced.AdvancedPlayer;
public class GameSound extends Thread{
//两个变量: musicpath和isloop
public String musicpath;//音乐位置
public boolean isloop; //●是否循环(用在do-while()中)
public GameSound(String musicpath, boolean isloop) {
this.musicpath = musicpath;
this.isloop = isloop;
}
public void run() {
try {
do{
InputStream mp3= GameSound.class.getClassLoader().getResourceAsStream("Music/"+musicpath);//★★★★★getResourceAsStream()音乐返回的类型不是URL,而是InputStream
AdvancedPlayer playmp3=new AdvancedPlayer(mp3);//创建一个播放器
playmp3.play();//开始播放
}while(isloop);//是否循环
} catch (Exception e) {
e.printStackTrace();
}
}
}
package Game;
import java.io.InputStream;
import javazoom.jl.player.advanced.AdvancedPlayer;
public class GameSound extends Thread{
//两个变量: musicpath和isloop
public String musicpath;//音乐位置
public boolean isloop; //●是否循环(用在do-while()中)
public GameSound(String musicpath, boolean isloop) {
this.musicpath = musicpath;
this.isloop = isloop;
}
public void run() {
try {
do{
InputStream mp3= GameSound.class.getClassLoader().getResourceAsStream("Music/"+musicpath);//★★★★★getResourceAsStream()音乐返回的类型不是URL,而是InputStream
AdvancedPlayer playmp3=new AdvancedPlayer(mp3);//创建一个播放器
playmp3.play();//开始播放
}while(isloop);//是否循环
} catch (Exception e) {
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-05-02
展开全部
得加JFrame界面吧
File musicFile=new File("所要添加的音乐名");
URI uri=musicFile.toURI();
URL url=uri.toURL():
AudioClip=Applet.newAudioClip(url);
控制音乐的播放与停止
clip.play();
clip.stop();
File musicFile=new File("所要添加的音乐名");
URI uri=musicFile.toURI();
URL url=uri.toURL():
AudioClip=Applet.newAudioClip(url);
控制音乐的播放与停止
clip.play();
clip.stop();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询