Java实现的简单音乐播放器功能示例
人气:4本文实例讲述了Java实现的简单音乐播放器功能。分享给大家供大家参考,具体如下:
应用名称:Java简单的音乐播放器
用到的知识:Java GUI编程,线程,IO
开发环境:win8+eclipse+jdk1.8
功能说明:可以选择内置的音乐文件播放,循环播放,停止。PS:这个播放器只能播放.au .aiff .wav .midi .rfm格式的音频。
效果图:
源代码:
import java.applet.AudioClip; import java.awt.*; import java.net.*; import java.awt.event.*; import java.io.*; import java.applet.*; import javax.swing.*; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; public class MusicPlayer extends JDialog implements Runnable,ItemListener,ActionListener{ /** * */ private static final long serialVersionUID = 1L; Thread thread; JComboBox<Serializable> choiceMusic; AudioClip clip; JButton buttonPlay,buttonLoop,buttonStop; String str; MusicPlayer(){ thread=new Thread(this); choiceMusic=new JComboBox(); choiceMusic.addItem("选择音频文件"); choiceMusic.addItem("src/1.wav"); choiceMusic.addItem("2.wav"); choiceMusic.addItem("3.wav"); choiceMusic.addItemListener(this); buttonPlay=new JButton("播放"); buttonLoop=new JButton("循环"); buttonStop=new JButton("停止"); buttonPlay.addActionListener(this); buttonLoop.addActionListener(this); buttonStop.addActionListener(this); setLayout(new FlowLayout()); add(choiceMusic); add(buttonPlay); add(buttonLoop); add(buttonStop); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setSize(350,80); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==buttonPlay) clip.play(); else if(e.getSource()==buttonLoop) clip.loop(); else if(e.getSource()==buttonStop) clip.stop(); } @Override public void itemStateChanged(ItemEvent arg0) { str=choiceMusic.getSelectedItem().toString(); if(!(thread.isAlive())){ thread=new Thread(this); } try{thread.start(); } catch(Exception ee){} } @Override public void run() { try { File file=new File(str); URI uri=file.toURI(); URL url=uri.toURL(); clip=Applet.newAudioClip(url); } catch (MalformedURLException e) { } } public static void main(String[] args) { MusicPlayer musicplayer=new MusicPlayer(); musicplayer.setVisible(true); } }
希望本文所述对大家java程序设计有所帮助。
您可能感兴趣的文章:
加载全部内容