import sun.audio.*;
专注于为中小企业提供网站建设、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业大竹免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了超过千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class Sound5
{
FileInputStream file;
BufferedInputStream buf;
public Sound5()
{
try
{
file=new FileInputStream("1.mid");
buf=new BufferedInputStream(file);
AudioStream audio=new AudioStream(buf);
AudioPlayer.player.start(audio);
}
catch (Exception e) {}
}
}
public class e8165 extends Frame implements ActionListener
{
e8165()
{
super("音频播放器");
setBounds(300,300,200,100);
setVisible(true);
Button btn=new Button("播放");
setLayout(new FlowLayout());
add(btn);
btn.addActionListener(this);
validate();
// Sound5 play = new Sound5();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(1); }
});
}
public void actionPerformed(ActionEvent e)
{
Sound5 play = new Sound5();
}
public static void main(String[] args)
{
new e8165();
}
}
//下载一个.mid文件命名为1,放在上述代码的包下
放音乐的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里哪添加?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();//停止播放
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.AWTException;
import java.awt.Frame;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class bofan_2 extends JFrame implements ActionListener
{
boolean looping=false;
File file1=null;
AudioClip sound1;
AudioClip chosenClip;
private JComboBox box1=null; //歌曲列表
private JButton butbofan=null; //播放
private JButton butboxhuan=null; //循环播放
private JButton buttinzi=null; //停止
private JButton butshan=null; //上一首
private JButton butzhantin=null; //暂停
private JButton butxia=null; //下一首
private TrayIcon trayIcon;//托盘图标
private SystemTray systemTray;//系统托盘
public bofan_2()
{
this.setSize(420,400);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(null);
box1=new JComboBox();
box1.addItem("伤心太平洋");
box1.addItem("劲爆的士高");
box1.addItem("老夫少妻");
box1.addItem("爱不再来");
box1.addItem("抽身");
box1.addItem("伤心城市");
box1.addItem("二零一二");
box1.addItem("精忠报国");
box1.addItem("秋沙");
box1.addItem("吻别");
box1.addItem("音乐疯起来");
box1.setBounds(10,20,150,20);
butbofan=new JButton("播放");
butbofan.addActionListener(this);
butbofan.setBounds(165,50,60,20);
butboxhuan=new JButton("循环播放");
butboxhuan.addActionListener(this);
butboxhuan.setBounds(230,50,90,20);
buttinzi=new JButton("停止");
buttinzi.setEnabled(false);
buttinzi.addActionListener(this);
buttinzi.setBounds(335,50,60,20);
butshan=new JButton("上一首");
butshan.addActionListener(this);
butshan.setBounds(165,90,80,20);
butzhantin=new JButton("暂停");
butzhantin.setEnabled(false);
butzhantin.addActionListener(this);
butzhantin.setBounds(250,90,60,20);
butxia=new JButton("下一首");
butxia.addActionListener(this);
butxia.setBounds(320,90,80,20);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(box1);
this.getContentPane().add(butbofan);
this.getContentPane().add(butboxhuan);
this.getContentPane().add(buttinzi);
this.getContentPane().add(butshan);
this.getContentPane().add(butzhantin);
this.getContentPane().add(butxia);
try {
UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel");
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (InstantiationException e)
{
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e)
{
e.printStackTrace();
}
setSize(450,450);
systemTray = SystemTray.getSystemTray();//获得系统托盘的实例
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
trayIcon = new TrayIcon(ImageIO.read(new File("004.jpg")));
systemTray.add(trayIcon);//设置托盘的图标,0.gif与该类文件同一目录
}
catch (IOException e1)
{
e1.printStackTrace();
}
catch (AWTException e2)
{
e2.printStackTrace();
}
this.addWindowListener(
new WindowAdapter(){
public void windowIconified(WindowEvent e)
{
dispose();//窗口最小化时dispose该窗口
}
});
trayIcon.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 2)//双击托盘窗口再现
setExtendedState(Frame.NORMAL);
setVisible(true);
}
});
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source== butbofan)
{
System.out.println((String) box1.getSelectedItem());
file1=new File((String) box1.getSelectedItem()+".wav");
butboxhuan.setEnabled(true);
buttinzi.setEnabled(true);
butzhantin.setEnabled(true);
butzhantin.setText("暂停");
try {
sound1 = Applet.newAudioClip(file1.toURL());
chosenClip = sound1;
} catch(OutOfMemoryError er){
System.out.println("内存溢出");
er.printStackTrace();
} catch(Exception ex){
ex.printStackTrace();
}
chosenClip.play();
this.setTitle("正在播放"+(String) box1.getSelectedItem());
}
if (source== butboxhuan)
{
file1=new File((String) box1.getSelectedItem()+".wav");
try {
sound1 = Applet.newAudioClip(file1.toURL());
chosenClip = sound1;
} catch(OutOfMemoryError er){
System.out.println("内存溢出");
er.printStackTrace();
} catch(Exception ex){
ex.printStackTrace();
}
looping = true;
chosenClip.loop();
butboxhuan.setEnabled(false);
buttinzi.setEnabled(true);
butzhantin.setText("暂停");
this.setTitle("正在循环播放"+(String) box1.getSelectedItem());
}
if (source== buttinzi)
{
if (looping)
{
looping = false;
chosenClip.stop();
butboxhuan.setEnabled(true);
butzhantin.setText("暂停");
} else {
chosenClip.stop();
}
buttinzi.setEnabled(false);
this.setTitle("停止播放");
}
if(source==butshan)
{
butzhantin.setText("暂停");
}
if(source==butzhantin)
{
buttinzi.setEnabled(false);
butzhantin.setText("继续");
if(source==butzhantin)
{
butzhantin.setText("暂停");
}
}
if(source==butxia)
{
butzhantin.setText("暂停");
}
}
public static void main(String[] args)
{
bofan_2 xx=new bofan_2();
}
}
/*
可以用加载声音文件的方法:
第一帧:mysound= new Sound();
mysound.attachSound(声音id名字);
ptime = 0;
播放按钮as:
on(release){
mysound.start(ptime);
}
暂停按钮as:
on(release){
ptime = mysound.position/1000;
mysound.stop();
}
*/