第一种方式:不带面板:

/**
 * 
 */
package com.niit.hitmouse;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 * @author: Annie
 * @date:2016年6月3日
 * @description:
 */
public class PlayMusic {

    public PlayMusic(String url) throws Exception{
        //给界面添加音乐
        File file = new File(url);//url为你音乐的路径
        AudioInputStream auIn = AudioSystem.getAudioInputStream(file);
        Clip clip = (Clip)AudioSystem.getClip();
        clip.open(auIn);
        clip.start();
        while(clip.available() > 0)
        {
            Thread.sleep(1000);
        }
        clip.close();
    }
    
}

 

 

第二种方式:带面板,可控制的

package com.niit.innerclass;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenuItem;

/**
 * @author: Annie
 * @date:2016年6月3日
 * @description:音乐播放器
 */
public class MusicDemo extends JDialog implements ActionListener,ItemListener,Runnable{

    JButton btn_play,btn_stop,btn_loop;
    JComboBox musicCombox;
    AudioClip clip;
    Thread thead;
    String str;


    public MusicDemo() {
        setSize(350, 200);
        setLayout(new FlowLayout());
        musicCombox = new JComboBox();
        musicCombox.addItem("选择音频文件");
        musicCombox.addItem("./src/梦中的婚礼.wav");
        musicCombox.addItem("./src/打击地鼠.wav");
        btn_play = new JButton("播放");
        btn_stop = new JButton("停止");
        btn_loop = new JButton("循环");
        thead  = new Thread(this);




        add(musicCombox);
        add(btn_play);
        add(btn_stop);
        add(btn_loop);

        musicCombox.addItemListener(this);
        btn_play.addActionListener(this);
        btn_stop.addActionListener(this);
        btn_loop.addActionListener(this);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {
        new MusicDemo();

    }


    public void run() {
        System.out.println("run方法进来了");
        try {
            File file = new File(str);//创建一个file对象
            URI uri = file.toURI();//创建一个URI对象
            URL url = uri.toURL();//创建一个URL对象
            clip = Applet.newAudioClip(url);//创建一个音频对象
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }


    @Override
    public void itemStateChanged(ItemEvent e) {
        System.out.println("itemStateChanged方法进来了");
        str = musicCombox.getSelectedItem().toString();
        System.out.println("----------str:"+str);
        if(!(thead.isAlive())){
            thead = new Thread(this);
        }
        try{
            thead.start();
        }catch(Exception ee){

        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("actionPerformed方法进来了");
        if(e.getSource() == btn_play){
            clip.play();
        }else if(e.getSource() == btn_stop){
            clip.stop();
        }else if(e.getSource() == btn_loop){
            clip.loop();
        }
    }

}

 

效果图:

 

第三种方式:

package com.niit.innerclass;

/**
 * @author: Annie
 * @date:2016年6月3日
 * @description:
 */

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Play{
    private String file_path="./src/梦中婚礼.wav";
    private AudioInputStream    ais; 
    private SourceDataLine      line; 
    private AudioFormat         baseFormat;
    private static final int    BUFFER_SIZE=4000*4;

    private SourceDataLine getLine(AudioFormat audioFormat) {
        SourceDataLine res = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class,audioFormat);
        try {
            res = (SourceDataLine) AudioSystem.getLine(info);
            res.open(audioFormat);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    private void play()throws UnsupportedAudioFileException, LineUnavailableException, IOException{
        ais= AudioSystem.getAudioInputStream(new File(file_path));
        baseFormat = ais.getFormat();

        line = getLine(baseFormat);
        line.start();
        int inBytes = 0;
        byte[] audioData=new byte[BUFFER_SIZE];    
        while (inBytes != -1){
            inBytes = ais.read(audioData, 0, BUFFER_SIZE);
            if (inBytes >= 0) {
                int outBytes = line.write(audioData, 0, inBytes);
            }
        }
    }
    public static void main(String args[])throws UnsupportedAudioFileException, LineUnavailableException, IOException
    {
        Play p1=new Play();
        p1.play();
    }
}

 

posted on 2016-06-03 16:39  王铭霞  阅读(687)  评论(1编辑  收藏  举报