JAVA-音乐播放器(声音)

一组简单的声音文件的应用

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.List;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MusicPlayer extends JFrame{

    private JLabel songNameLabel=null;//显示当前音乐的播放的状态:播放和停止
    private JButton btnLast=null;//切换到上一首音乐的按钮
    private JButton btnPay=null;//播放当前音乐的按钮
    private JButton btnNext=null;//切换到下一首音乐的按钮
    private JButton btnLoop=null;//音乐循环的按钮

    private List songList=null;//显示歌曲的列表,List导入awt内的包,util内为集合
    //音乐文件名数组
    String strSongNames[]={"song1.wav","song2.wav","song3.wav","song4.wav","song5.wav","song6.wav"};//
    int len=strSongNames.length;//长度
    final String DIR="songs\\";//路径
    AudioClip songs[];//音乐对象数组
    AudioClip currentSong;//标记当前歌曲
    int index;//歌曲的序号
    boolean isPlayOrStop=false;//标记播放状态,
    boolean isLoop=false;//标记是否循环

    class MouseListner implements MouseListener{//监听

        public void mouseClicked(MouseEvent e) {
            if(currentSong!=null){//防卫第一次点击按钮,currentSong为空
                currentSong.stop();//如果当前歌曲正在播放,则停止
            }
            if(e.getSource()==btnLast){
                index--;
                if(index<0){
                    index+=len;
                }
            }else if(e.getSource()==btnPay){
                isPlayOrStop=!isPlayOrStop;
            }else if(e.getSource()==btnNext){
                index++;
                if(index>=len){
                    index=index%len;
                }
            }else{
                isLoop=!isLoop;
            }
            songList.select(index);//播放的音乐被选中
            if(isPlayOrStop){//如果处于播放状态
                MusicRun mr=new MusicRun();
                Thread t=new Thread(mr);
                t.start();//启动播放线程
            }else{
                btnPay.setIcon(new ImageIcon("images2\\2.png"));//因为音乐停止播放,所以停止图片按钮变成播放图片按钮
                String strCurrentSong=strSongNames[index];
                songNameLabel.setText(strCurrentSong+"停止播放");//设置显示停止播放状态
            }
        }
        @Override
        public void mousePressed(MouseEvent e) {

        }
        @Override
        public void mouseReleased(MouseEvent e) {
        }
        @Override
        public void mouseEntered(MouseEvent e) {
        }
        @Override
        public void mouseExited(MouseEvent e) {         
        }
    }

    class MusicRun implements Runnable{//播放线程
        @Override
        public void run() {
            currentSong=songs[index];
            if(isLoop){
                currentSong.loop();
            }
            if(isPlayOrStop){
                currentSong.play();
            }
            String strCurrentSongName=strSongNames[index];
            songNameLabel.setText("正在播放"+strCurrentSongName);//显示音乐正在播放状态
            btnPay.setIcon(new ImageIcon("images2\\5.png"));//因为音乐正在播放,所以播放图片按钮变成停止图片按钮
        }
    }

    public MusicPlayer() {
        super("音乐播放器");
        this.setBounds(300, 50, 310, 480);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);//设置界面大小不可变
        this.setLayout(null);//***空布局--绝对布局:组件必须设置位置和大小***

        songNameLabel=new JLabel("我的音乐播放器");//显示当前音乐的播放的状态:播放和停止
        Font songNameFont=new Font("黑体",Font.ITALIC,18);
        songNameLabel.setFont(songNameFont);
        songNameLabel.setBounds(10,10,300,40);
        getContentPane().add(songNameLabel);

        //四个播放键功能
        btnLast=new JButton();
        btnPay=new JButton();
        btnNext=new JButton();
        btnLoop=new JButton();
        btnLast.setBounds(10, 70, 50, 40);//位置 大小
        btnPay.setBounds(70, 70, 50, 40);
        btnNext.setBounds(130, 70, 50, 40);
        btnLoop.setBounds(190, 70, 50, 40);
        btnLast.setIcon(new ImageIcon("images2\\1.png"));//对按钮设置图片
        btnPay.setIcon(new ImageIcon("images2\\2.png"));
        btnNext.setIcon(new ImageIcon("images2\\3.png"));
        btnLoop.setIcon(new ImageIcon("images2\\4.png"));
        getContentPane().add(btnLast);
        getContentPane().add(btnPay);
        getContentPane().add(btnNext);
        getContentPane().add(btnLoop);
        MouseListner ml=new MouseListner();//添加监听
        btnLast.addMouseListener(ml);
        btnPay.addMouseListener(ml);
        btnNext.addMouseListener(ml);
        btnLoop.addMouseListener(ml);

        //音乐播放列表
        JLabel listLabel=new JLabel("播放列表");
        listLabel.setBounds(10, 120, 100, 30);
        Font listLabelFont=new Font("宋体", Font.BOLD, 15);
        getContentPane().add(listLabel);

        songList=new List();
        songList.setBounds(10, 150, 260, 260);
        songList.setBackground(Color.CYAN);
        songs=new AudioClip[len];
        for(int i=0;i<len;i++){
            songList.add(strSongNames[i]);//把音乐添加到列表中去,用于在界面上的显示
            songs[i]=loadSong(DIR+strSongNames[i]);//用于播放
        }
        getContentPane().add(songList);

        index=(int)(Math.random()*100)%len;//一开始播放时,是随机选择的
        songList.select(index);
        setVisible(true);
    }
    private AudioClip loadSong(String fileName) {//把音乐依次逐个加入songs[]中
        URL url=null;
        try {
            url=new URL("file:"+fileName);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return Applet.newAudioClip(url);
    }
    public static void main(String[] args) {

        new MusicPlayer();
    }

}

按钮图标
1.png2.png3.png4.png5.png
声音文件
链接: http://pan.baidu.com/s/1eSjqK8a 密码: qbz5
运行界面
这里写图片描述
这里写图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值