JAVA-音乐播放器(声音)
2016-06-20 16:18
302 查看
一组简单的声音文件的应用
按钮图标
声音文件
链接: http://pan.baidu.com/s/1eSjqK8a 密码: qbz5
运行界面
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(); } }
按钮图标
声音文件
链接: http://pan.baidu.com/s/1eSjqK8a 密码: qbz5
运行界面
相关文章推荐
- Java设计模式之--桥接模式(bridge pattern)
- Spring整合JMS(消息中间件)实例
- java泛型方法
- Eclipse导出安卓apk文件的图文教程
- [疯狂Java]正则表达式:捕获组、反向引用、捕获组命名
- TestNG 集成Spring-test
- springMVC数据绑定之日期属性编辑器(最好用)
- springMVC数据绑定之日期属性编辑器(最好用)
- 获取jar包中的目录及文件
- TestNG单元测试
- javaEE-EJB学习笔记
- Spring遇到 NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
- Ubuntu14.04 安装Eclipse
- JAVA IO之装饰器模式(Head first 设计模式之装饰器模式)
- Spring aop读写分离
- JAVA 对象大小
- retrofit2+rxjava+mockserver使用和理解
- Java基础学习总结——Java对象的序列化和反序列化
- java对象和json对象互相转换
- java Exception in thread "main" java.lang.NoClassDefFoundError: (wrong name: ) 解决方案