JAVA音乐播放器 文档

合集下载

Java课程设计音乐播放器设计

Java课程设计音乐播放器设计

程序设计课程设计综合实验音乐播放器班级:指导老师:组员:2014年12月2日目录1 程序功能描述 -------------------------------------------------------------------------------- 32 开发环境描述 -------------------------------------------------------------------------------- 33 开发技术介绍 -------------------------------------------------------------------------------- 34 详细设计--------------------------------------------------------------------------------------- 4 4.1功能模块划分 ------------------------------------------------------------------------------------ 4 4.2 用户界面设计 ----------------------------------------------------------------------------------- 54.2.1 歌曲列表面板 --------------------------------------------------------------------------------------------- 54.2.2 播放控制面板 --------------------------------------------------------------------------------------------- 84.2.3 搜索及展示面板 ------------------------------------------------------------------------------------------ 84.3 播放功能实现 ----------------------------------------------------------------------------------- 94.3.1播放歌曲 ---------------------------------------------------------------------------------------------------- 94.3.2 暂停及继续播放 ---------------------------------------------------------------------------------------- 114.3.3 音量控制 ------------------------------------------------------------------------------------------------- 124.3.4 播放模式 ------------------------------------------------------------------------------------------------- 124.3.5 时间进度条 ---------------------------------------------------------------------------------------------- 134.4 歌词展示实现 --------------------------------------------------------------------------------- 144.4.1 加载歌词文件 ------------------------------------------------------------------------------------------- 144.4.2 解析歌词文件 ------------------------------------------------------------------------------------------- 144.4.3 展示歌词 ------------------------------------------------------------------------------------------------- 154.5 搜索网络歌曲资源---------------------------------------------------------------------------- 164.5.1 获取HTML文本 --------------------------------------------------------------------------------------- 164.5.2 解析HTML文本 --------------------------------------------------------------------------------------- 184.5.3 抓取数据描述 ------------------------------------------------------------------------------------------- 204.6 网络歌曲资源处理---------------------------------------------------------------------------- 204.6.1 歌曲资源的载体 ---------------------------------------------------------------------------------------- 204.6.2 歌曲资源的操作 ---------------------------------------------------------------------------------------- 244.7 程序内置的游戏------------------------------------------------------------------------------- 264.7.1 2048 -------------------------------------------------------------------------------------------------------- 264.7.2 贪吃蛇 ---------------------------------------------------------------------------------------------------- 274.7.3 五子棋 ---------------------------------------------------------------------------------------------------- 275程序运行 -------------------------------------------------------------------------------------- 28 6 实验小结-------------------------------------------------------------------------------------- 311 程序功能描述音乐播放器是一种用于播放各种音乐文件的多媒体播放软件。

java音乐播放器实现代码

java音乐播放器实现代码

java⾳乐播放器实现代码本⽂实例为⼤家分享了java⾳乐播放器的具体代码,供⼤家参考,具体内容如下这个是源码结构介绍这个是界⾯,有点简陋,见笑了,但是基本上的东西都有了,没办法,没有美⼯的程序写的界⾯直接上源代码Player.javapackage com.service;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.Random;import javax.sound.sampled.*;import javax.swing.JSlider;import javax.swing.JTable;import com.list.MusicList;import com.list.ThreadList;import com.list.ViewList;import com.model.Model;import com.model.Music;import com.view.View;/*"duration""author""title""copyright"private Player p;private long time = 0;Object lock = new Object();//⼀个空的对象,没什么意义private boolean paused = false;// 暂停继续public boolean isPaused() {return paused;}public void setPaused(boolean paused) {this.paused = paused;}private JSlider jSliderPlayProgress;//播放进度条private boolean over = false;//开始结束//是否⾃动播放下⼀曲private boolean isNext=true;private Music music;//⾳乐AudioInputStream din = null;SourceDataLine line=null;private FloatControl volume = null;private JSlider jSliderVolume;public JSlider getjSliderVolume() {return jSliderVolume;}public void setjSliderVolume(JSlider jSliderVolume) {this.jSliderVolume = jSliderVolume;}public Player(JSlider jSliderVolume,JSlider jSliderPlayProgress) { super();this.jSliderVolume = jSliderVolume;this.jSliderPlayProgress=jSliderPlayProgress;}public Music getMusic() {return music;}public void setMusic(Music music) {this.music = music;}public FloatControl getVolume(){return volume;}//播放⾳乐public void run(){AudioInputStream in=null;try {File file = new File(music.getPath());//播放不了的歌曲,直接下⼀⾸,并且在⾳乐列表中删除try {ViewList.getList().get(0).getJt().setModel(new Model());nextmusic();}AudioFormat baseFormat = in.getFormat();AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,baseFormat.getSampleRate(), 16, baseFormat.getChannels(),baseFormat.getChannels() * 2, baseFormat.getSampleRate(),false);if(baseFormat.getEncoding()==AudioFormat.Encoding.PCM_UNSIGNED || baseFormat.getEncoding()==AudioFormat.Encoding.ULAW || baseFormat.getEncoding()==AudioFormat.Encoding.ALAW || baseFormat.getEncoding()==AudioFormat.Encoding.PCM_SIGNED){time=(file.length()*8000000)/((int)(decodedFormat.getSampleRate()*baseFormat.getSampleSizeInBits()));}else{int bitrate=0;if(baseFormat.properties().get("bitrate")!=null){//取得播放速度(单位位每秒)bitrate=(int)((Integer)(baseFormat.properties().get("bitrate")));if(bitrate!=0)time=(file.length()*8000000)/bitrate;}}din = AudioSystem.getAudioInputStream(decodedFormat, in); info = new (SourceDataLine.class, decodedFormat);line = (SourceDataLine) AudioSystem.getLine(info);line.open();setVolume();jSliderPlayProgress.setMaximum((int)time);jSliderPlayProgress.setValue(0);if(line!=null){line.open(decodedFormat);byte[] data = new byte[4096];int nBytesRead;synchronized (lock) {while ((nBytesRead = din.read(data, 0, data.length)) != -1) {while (paused) {if(line.isRunning()) {line.stop();System.out.println("暂停");}try {lock.wait();System.out.println("等待");}catch(InterruptedException e) {}}if(!line.isRunning()&&!over) {System.out.println("开始播放");line.start();}if (over&&line.isRunning()) {System.out.println("停⽌播放");jSliderPlayProgress.setValue(0);isNext=false;line.drain();line.stop();line.close();}jSliderPlayProgress.setValue((int)line.getMicrosecondPosition());line.write(data, 0, nBytesRead);}//根据播放模式选择下⼀⾸歌nextmusic();}}finally {if(din != null) {try { din.close(); } catch(IOException e) { }}}}//设置播放器滚动条public void setVolume(){if(line!=null){if(line.isControlSupported(FloatControl.Type.MASTER_GAIN)){jSliderVolume.setEnabled(true);volume= (FloatControl)line.getControl( FloatControl.Type.MASTER_GAIN );jSliderVolume.setMinimum((int)volume.getMinimum());jSliderVolume.setMaximum((int)volume.getMaximum());//jSliderVolume.setValue((int)(volume.getMinimum()+(4*(volume.getMaximum()-volume.getMinimum()))/5)); volume.setValue((float)(volume.getMinimum()+(4*(volume.getMaximum()-volume.getMinimum()))/5));}}else{volume=null;jSliderVolume.setEnabled(false);}}private void nextmusic() {String mode=Setting.getMode();if (isNext&&!mode.equals("one")) {//单曲播放就不执⾏int nextid=0;//将要播放的idint currentid=Integer.parseInt(this.music.getId());System.out.println(mode);if (mode.equals("default")&&(currentid==MusicList.getList().size()-1)){return;}if (mode.equals("rand")) {Random random = new Random();nextid=Math.abs(random.nextInt())%MusicList.getList().size();}else if (mode.equals("onecircle")) {nextid=currentid;}else if (mode.equals("default")&&!(currentid==MusicList.getList().size()-1)) {nextid=currentid+1;}else if (mode.equals("morecircle")) {nextid=(currentid==MusicList.getList().size()-1)?0:currentid+1;}JTable jTable=ViewList.getList().get(0).getJt();if(nextid==0){//第⼀个jTable.setRowSelectionInterval(0,0);}else {jTable.setRowSelectionInterval(nextid-1,nextid);}this.stopplay();ThreadList.getList().clear();p=new Player(jSliderVolume,jSliderPlayProgress);p.setMusic(MusicList.getList().get(nextid));ThreadList.getList().add(p);p.start();}}//开始public void startplay(){over=false;}//停⽌public void stopplay(){// 暂停public void userPressedPause() {paused = true;}//继续public void userPressedPlay() {synchronized(lock) {paused = false;lock.notifyAll();}}public void Pause(){if (paused) {synchronized(lock) {paused = false;lock.notifyAll();}}else{paused = true;}}}这个主要是播放⾳乐的类,播放,暂停,停⽌,上⼀⾸,下⼀⾸都有了 View.javapackage com.view;import java.awt.BorderLayout;import java.awt.Color;import ponent;import java.awt.Container;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.io.File;import java.io.IOException;import .URI;import .URL;import java.util.ArrayList;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JRootPane;import javax.swing.JScrollPane;import javax.swing.JSlider;import javax.swing.JTable;import javax.swing.ListSelectionModel;import javax.swing.ScrollPaneConstants;import javax.swing.SwingConstants;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.table.TableCellRenderer;import com.list.ViewList;import com.model.Model;import com.model.Music;import com.service.Player;import com.service.Setting;import com.util.DirInput;import com.util.FileInput;import com.util.FileList;import com.util.List_File;public class View extends JFrame implements MouseListener,ActionListener,WindowListener { private JButton stop, open,del,next,pre;private Player p;private JPanel[] jPanels;private MusicList list;//private Long clickTime=0l;private JScrollPane jsp;private JTable jt;private JRootPane j;private Model model;private JSlider jSliderVolume;private JSlider jSliderPlayProgress;private FileInput fileinput;private DirInput dirInput;private JMenuBar jb;private JMenu jm;private JMenuItem fm,dm;private JComboBox jBox;public View(){System.out.println(ViewList.getList().size());if (ViewList.getList().size()==0) {Open();}}private void Open() {//this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);//this.setUndecorated(true);//菜单选项jb=new JMenuBar();jm=new JMenu("打开");fm=new JMenuItem("⽂件");dm=new JMenuItem("⽂件夹");fm.addActionListener(this);dm.addActionListener(this);jb.add(jm);jm.add(fm);jm.add(dm);//this.setJMenuBar(jb);JPanel p1=new JPanel();JPanel p2=new JPanel();JPanel p3=new JPanel();//增加菜单选项j=new JRootPane();j.setJMenuBar(jb);open=new JButton("播放");stop=new JButton("停⽌");open.addMouseListener(this);stop.addMouseListener(this);pre=new JButton("上⼀⾸");next=new JButton("下⼀⾸");pre.addMouseListener(this);next.addMouseListener(this);p1.setLayout(new GridLayout(2,1));JPanel jPanel2=new JPanel();jPanel2.add(open);jPanel2.add(stop);jPanel2.add(pre);jPanel2.add(next);p1.add(jPanel2);/*p1.add(open);p1.add(stop);p1.add(pre);p1.add(next);*/del=new JButton("删除");del.addMouseListener(this);jSliderPlayProgress = new JSlider(); //播放进度条jSliderPlayProgress.setValue(0);jSliderPlayProgress.setEnabled(false);jSliderPlayProgress.setPreferredSize(new Dimension(200, 20));p1.add(jSliderPlayProgress);jSliderVolume = new JSlider(); //⾳量进度条jSliderVolume.setValue(0);//jSliderPlayProgress.setEnabled(false);jSliderVolume.setPreferredSize(new Dimension(100, 20));//设置滚动条长度jSliderVolume.addChangeListener(new ChangeListener(){public void stateChanged(ChangeEvent evt){System.out.println(jSliderVolume.getValue());if (ThreadList.getList().size()!=0) {ThreadList.getList().get(0).getVolume().setValue((float)jSliderVolume.getValue()); }}});String[] v={"顺序播放","随机播放","单曲循环","列表循环","单曲播放"};jBox=new JComboBox(v);jBox.addActionListener(this);p2.add(jBox);p2.add(del);p2.add(jSliderVolume);jPanels=new JPanel[list.getList().size()];for (int i = 0; i < list.getList().size(); i++) {Music music=list.getList().get(i);JPanel jPanel=new MyJPanel(music);JLabel jLabel=new JLabel(music.getName(),SwingConstants.CENTER);jLabel.setSize(300, 10);jPanels[i]=jPanel;jPanel.addMouseListener(this);jPanel.add(jLabel);p3.add(jPanel);}p3.setBackground(Color.WHITE);p3.setLayout(new GridLayout(10, 1));p3.setSize(320, 500);this.add(p1,BorderLayout.NORTH);this.add(p2,BorderLayout.SOUTH);model=new Model(); //添加表jt=new JTable(model){ // 设置jtable的单元格为透明的public Component prepareRenderer(TableCellRenderer renderer,int row, int column) {Component c = super.prepareRenderer(renderer, row, column);if (c instanceof JComponent) {((JComponent) c).setOpaque(false);}return c;}};;jt.setOpaque(false);jt.setRowHeight(30);jt.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );jt.setShowHorizontalLines(false);jt.setSelectionBackground(new Color(189,215,238));jt.addMouseListener(this);jsp = new JScrollPane(jt);jsp.setOpaque(false);jsp.getViewport().setOpaque(false);//addmusic();//this.add(p3,BorderLayout.CENTER);this.add(jsp,BorderLayout.CENTER);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.addWindowListener(this);Image image=this.getToolkit().getImage("img/icon.jpg");this.setIconImage(image);this.setTitle("⾳乐播放器");ImageIcon icon = new ImageIcon("img/bg.jpg");JLabel lab = new JLabel(icon); // 将图⽚放⼊到label中lab.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight()); // 设置放有图⽚的label的位置this.getContentPane().add(lab, -1); // jthis本⾝是窗体,不能放置任何组件,⽤getContentPane()⽅法得到this的默认内容⾯板,将lab放⼊其中,-1表⽰放⼊⾯板的下层jSliderVolume.setOpaque(false);jPanel2.setOpaque(false);p1.setOpaque(false);p3.setOpaque(false);jSliderPlayProgress.setOpaque(false);this.setLocation(400, 200);this.setSize(337, 525);this.setResizable(false);this.setVisible(true);}@Overridepublic void mouseClicked(MouseEvent e) {System.out.println("开始播放");if (e.getSource()==open) {if (p==null) {//开始p=new Player(jSliderVolume,jSliderPlayProgress);p.setMusic(MusicList.getList().get(0));jt.setRowSelectionInterval(0,0);ThreadList.add(p);open.setText("暂停");p.start();}else{//继续if (ThreadList.getList().size()!=0) {p=ThreadList.getList().get(0);}String s=p.isPaused()?"暂停":"播放";open.setText(s);p.Pause();}}else if (e.getSource()==stop) {if (ThreadList.getList().size()!=0) {p=ThreadList.getList().get(0);}if (p!=null) {p.stopplay();p=null;open.setText("播放");}}else if (e.getSource()==pre) {//上⼀⾸premusic();}else if (e.getSource()==next) {//下⼀⾸nextmusic();}else if (e.getSource()==del) {delmusic();}else if (e.getSource()==jt&&e.getClickCount()==2) {//双击 clickmusic();}}private void clickmusic() {//双击JtableSystem.out.println("点击了");int rowNum = this.jt.getSelectedRow();System.out.println(rowNum);if(rowNum == -1) {JOptionPane.showMessageDialog(this, "你没有选择⼀项"); return;}ArrayList<Player> list=ThreadList.getList();p=new Player(jSliderVolume,jSliderPlayProgress);p.setMusic(MusicList.getList().get(rowNum));ThreadList.add(p);open.setText("暂停");p.start();}else{System.out.println("停⽌");list.get(0).stopplay();list.clear();p=new Player(jSliderVolume,jSliderPlayProgress);p.setMusic(MusicList.getList().get(rowNum));open.setText("暂停");list.add(p);p.start();}}private void delmusic() {int rowNum = this.jt.getSelectedRow();MusicList.getList().remove(rowNum);System.out.println(MusicList.getList().size());jt.setModel(new Model());ArrayList<Player> list=ThreadList.getList();p=new Player(jSliderVolume,jSliderPlayProgress);System.out.println(list.size()+"⼤⼩");if (list.size()!=0) {list.get(0).stopplay();list.clear();open.setText("暂停");if(rowNum==0){//第⼀个System.out.println("第⼀个");jt.setRowSelectionInterval(0,0);p.setMusic(MusicList.getList().get(rowNum));}else if(rowNum==MusicList.getList().size()){//最后⼀个 System.out.println("最后⼀个");jt.setRowSelectionInterval(rowNum-2,rowNum-1);p.setMusic(MusicList.getList().get(rowNum-1));}else {System.out.println("中间");jt.setRowSelectionInterval(rowNum-1,rowNum);p.setMusic(MusicList.getList().get(rowNum));}list.add(p);p.start();}}public JTable getJt() {return jt;}private void premusic() {System.out.println("上⼀⾸");ArrayList<Player> list=ThreadList.getList();int id=Integer.parseInt(list.get(0).getMusic().getId());if(id!=0){if (id==1) {jt.setRowSelectionInterval(0,0);}else{jt.setRowSelectionInterval(id-2,id-1);}System.out.println(id);p=new Player(jSliderVolume,jSliderPlayProgress); p.setMusic(MusicList.getList().get(id-1));System.out.println(id-1);open.setText("暂停");list.add(p);p.start();}}private void nextmusic() {System.out.println("下⼀⾸");ArrayList<Player> list=ThreadList.getList();int id=Integer.parseInt(list.get(0).getMusic().getId()); System.out.println(id);if(id!=MusicList.getList().size()-1){ //122jt.setRowSelectionInterval(id,id+1); //123条list.get(0).stopplay();list.clear();p=new Player(jSliderVolume,jSliderPlayProgress); p.setMusic(MusicList.getList().get(id+1));System.out.println(id+1);open.setText("暂停");list.add(p);p.start();}}//判断双击/* private boolean checkClickTime() {long nowTime = (new Date()).getTime();if ((nowTime - clickTime) < 300) {clickTime = nowTime;return true;}clickTime = nowTime;return false;}*/private void addmusic(String path) {//增加mp3⽂件夹 System.out.println("增加mp3⽂件夹");ArrayList<Music> musiclist=MusicList.getList();List_File fm = new List_File();ArrayList<String[]> FileList = fm.serachFiles(path); for (int i = 0; i < FileList.size(); i++) {Music music= new Music();music.setId(musiclist.size()+"");String[] s=(String[]) FileList.get(i);music.setName(s[0]);music.setPath(s[1]);musiclist.add(music);}jt.setModel(new Model());}@Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getSource()==fm) {//添加mp3⽂件if(fileinput==null) fileinput = new FileInput(this);fileinput.open();File[] s=fileinput.getFiles();ArrayList<Music> musiclist=MusicList.getList();if(s!=null){for(int i=0;i<s.length;i++){Music music= new Music();music.setId(musiclist.size()+"");music.setName(s[i].getName());music.setPath(s[i].getAbsolutePath());musiclist.add(music);jt.setModel(new Model());}}}else if (e.getSource()==dm) {if(dirInput==null) dirInput = new DirInput(this);dirInput.open();File s=dirInput.getFile();if(s!=null){addmusic(s.getAbsolutePath());}}else if (e.getSource()==jBox) {//顺序播放 (默认)default 随机rand 单曲循环 onecircle 列表循环 morecircle 单曲播放 one if (ThreadList.getList().size()!=0) {p=ThreadList.getList().get(0);}else {p=new Player(jSliderVolume,jSliderPlayProgress);ThreadList.getList().add(p);}String[] s={"default","rand","onecircle","morecircle","one"};Setting.setMode(s[jBox.getSelectedIndex()]);}}@Overridepublic void windowActivated(WindowEvent e) {// TODO Auto-generated method stub}@Overridepublic void windowClosed(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println("关闭kk");}@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println("close");if (MusicList.getList().size()!=0) {System.out.println("写⼊⽂件");//清空之前的内容FileList.clear("file/musiclist.txt");ArrayList<Music> list=MusicList.getList();for (int i = 0; i < list.size(); i++) {FileList.writeFile("file/musiclist.txt",list.get(i).getId()+","+list.get(i).getName()+","+list.get(i).getPath()+"\n");}}}@Overridepublic void windowDeactivated(WindowEvent e) {// TODO Auto-generated method stub}@Overridepublic void windowDeiconified(WindowEvent e) {// TODO Auto-generated method stub}@Overridepublic void windowIconified(WindowEvent e) {// TODO Auto-generated method stub}@Overridepublic void windowOpened(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println("open");File file=new File("file/musiclist.txt");if (file.exists()==false) {try {file.createNewFile();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}else {FileList.readFileByLines("file/musiclist.txt");jt.setModel(new Model());}}}剩下的代码在后⾯附件上有,现在主要是有⼏个⼩问题,第⼀,我还没有找到获取⾳乐⽂件具体信息⽐较好的办法,所以每⼀⾸暂时还没有歌⼿,作曲的信息,第⼆界⾯有点难看,见谅了,第三个是打包成jar⽂件会有路径问题,暂时还没办法解决,我是直接在myeclipse上运⾏,⼀切正常,就是打包有点⼩问题暂时先说这些了,这个是我业余时间的项⽬,有什么不⾜的,⼤家都可以提出来。

Java音频播放器需求分析

Java音频播放器需求分析

目录1.引言 (1)1.1编写目的 (1)1.2项目背景 (1)1.3项目目的和目标 (1)1.4参考资料 (1)2.系统需求分析 (2)2.1功能需求 (2)2.2界面需求 (8)2.3性能需求 (9)2.4可靠性需求 (9)2.5运行环境需求 (9)1.引言1.1编写目的为明确软件需求、安排项目规划与进度、组织软件开发与测试,撰写本文档。

本文档供项目经理、设计人员、开发人员参考。

1.2项目背景本项目由梁柏光等人提出,由本组成员联合开发,实现播放现今主流的音乐文件格式。

开发软件名称:JUST音频播放器项目任务提出者:梁柏光项目开发者:第四项目小组1.3项目目的和目标现今社会生活紧张,而欣赏音乐是其中最好的舒缓压力的方式之一,本项目的目的是开发一个可一播放主流的音乐文件格式,本设计的实现的主要功能是播放MP3,WA V, OGG ,APE ,LACF多种格式的音乐文件,并且能够控制播放,暂停,停止,音量控制的调节,选择上一曲,选择下一曲,视觉外观,列表文件和歌曲文件的管理操作等多种播放控制功能,界面简明,操作简单。

1.4参考资料a.可行性分析b.<<软件工程实践教程》赵池龙等编,电工出版社出版2系统需求分析根据项目的目标,我们可获得项目系统的基本需求,以下从不同角度来描述系统的需求,并且使用用例图来描述.2.1功能需求系统的功能需求,我们分成三部分来概括,即播放器的基本控制需求,播放列表管理需求和播放器友好性需求。

以下分别描述:一:播放器的基本控制需求用列规约:二:播放列表管理需求当用户选定某一播放列表,应有的需求用例名称:播放歌曲文件参与者:用户三:播放器友好性需求用例名称:视觉外观显示参与者:系统或用户目标:使得播放器根据歌曲文件的特性,通过视觉外观来动态显示前置条件:播放器正在播放歌曲文件基本事件流:1.系统读取要播放的歌曲文件的特性,2接着调用一个视觉外观显示组件来动态显示出来后置条件:视觉外观显示组件来动态显示播放中的歌曲文件用例名称:歌曲时间显示参与者:系统目标:使得播放器能显示歌曲文件的播放时间,并通过视觉数字动态显示前置条件:播放器播放歌曲文件基本事件流:1.系统读取要播放的歌曲文件的时间,2并通过视觉数字动态显示已播放了歌曲文件多长时间后置条件:视觉数字显示动态显示播放中的歌曲文件的时间用例名称:播放状态显示参与者:系统目标:使得播放器能显示播放器现时的状态,是播放还是暂停中前置条件:播放器已打开基本事件流:1.系统读取播放器状态,2通过文字标签显示出来后置条件:文字标签将播放器状态显示出来用例名称:歌曲文件信息显示参与者:系统目标:使得用户能了解歌曲文件的信息,例如标题,歌手,声道,比特率等信息前置条件:播放器播放歌曲文件基本事件流:1.系统读取歌曲文件的信息,2并且通过文字标签显示出来后置条件:文字标签将歌曲文件状态显示出来2.2界面需求播放器界面要求布局合理,颜色舒适,控制按钮友好;为了减少开发工程量,我们借鉴了现时流行的千千静听的皮肤来作为我们播放器的界面目标需求,此皮肤版权归原作者所有,我们只是作学习所用,如下图:2.3性能需求(1)当要求歌曲播放时,软件响应时间最长不能超过2秒(2)当要求歌曲暂停是,软件响应时间最长不能超过1秒(3)播放歌曲,采用立体声播放,文件流畅2.4可靠性需求系统能持续运行,播放歌曲文件。

(完整word版)基于java的音乐播放器的设计

(完整word版)基于java的音乐播放器的设计

《课程设计报告》课程名称:WEB课程设计2016—2017 学年度第一学期题目:基于java的音乐播放器系别:信息工程学院专业: 计算机科学与技术专业班级:指导教师:编者目录一。

需求分析 (1)(一)背景 (1)(二)音乐播放器需求分析 (1)二.概要设计 (5)(一)系统所用类及各类的主要说明 (5)(二)系统UML类图 (6)三。

详细设计 (8)(一)核心技术介绍 (8)(二)创建播放器的图形用户界面所需的控件 (11)四。

系统实现 (14)五.测试 (44)结论 (46)一.需求分析(一)背景随着计算机的广泛运用,手机市场的迅速发展,各种音频视频也在网上广为流传,这些资源看似平常,但以渐渐成为人们必不可少的一部分了,于是各种手机播放器也紧跟着发展起来,但是很多播放器一味追求外观花哨,功能庞大,对很多用户的时间造成了很多资源的浪费,比如CPU、内存的占有率过高,在用户需求多用户操作时,受到了不小的影响,带来了许多的不便,而对于大多数普通用户,许多功能用不上,形同虚设。

(二)音乐播放器需求分析根据项目,我们可以获得项目系统的基本需求主要是功能需求而系统的功能需求,我们分成三部分来概括,即播放器的基本控制需求、播放列表管理需求和性能需求。

以下分别描述:121、播放器的基本控制需求(1)播放:点击播放列表中的歌曲进行播放。

(2)停止:在歌曲列表下方有一个小按钮,当歌曲正在播放时,显示的是可停止的按钮,点击则停止当前播放的歌曲.(3)上一曲:歌曲在播放或者未播放时单击上一曲按钮时歌曲会跳到列表的当前歌曲的上一首.(4)下一曲:歌曲在播放或者未播放时单击下一曲按钮时歌曲会跳到列表的当前歌曲的下一首。

(5)停止:单击停止按钮时立即停止播放当前的歌曲。

3用户需求图音乐播放器系统流程图2、播放列表管理(1)添加歌曲文件夹:在播放列表中单击,选择添加的文件夹则弹出一个添加文件夹的会话框,点击要添加的歌曲,则弹出“添加成功"的消息提示,否则弹出“添加失败”的相关信息。

Java程序设计课程设计报告音乐播放器

Java程序设计课程设计报告音乐播放器

《Java及Java web应用开发》实验报告设计题目: Java音乐播放器专业计算机信息管理班级 1班****2014年12月24 日import java*.media.ControllerEvent;import java*.media.ControllerListener;import java*.media.EndOfMediaEvent;import java*.media.PrefetchpleteEvent;import java*.media.RealizepleteEvent;import java*.media.*;import java*.swing.*;import java.awt.*;import java.awt.event.*;public class MediaPlayer e*tends JFrame implements ActionListener, ItemListener, ControllerListener {String title;Player player;boolean first = true, loop = false;ponent vc, cc;String currentDirectory=null;// 构造函数,其中包括了设置响应窗口事件的监听器。

MediaPlayer(String title) {super(title);/* 关闭按钮的实现。

*/addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {dispose(); }public void windowClosed(WindowEvent e) {if (player != null)player.close();System.e*it(0);} }); // 调用程序菜单栏的方法成员完成菜单的布置setupMenu();setSize(400, 400);setVisible(true); } // 本方法用以设置程序菜单栏public void setupMenu() {// 设置一个菜单Menu f = new Menu("文件");// 往设置的菜单添加菜单项MenuItem mi = new MenuItem("打开");f.add(mi);mi.addActionListener(this);f.addSeparator();Checkbo*MenuItem cbmi = new Checkbo*MenuItem("循环", false); cbmi.addActionListener(this);f.add(cbmi);f.addSeparator();MenuItem ee = new MenuItem("退出");ee.addActionListener(this);f.add(ee);f.addSeparator();Menu l = new Menu("播放列表");Menu c = new Menu("播放控制");MenuItem move = new MenuItem("播放");move.addActionListener(this);c.add(move);c.addSeparator();MenuItem pause = new MenuItem("暂停");pause.addActionListener(this);c.add(pause);c.addSeparator();MenuItem stop = new MenuItem("停止");stop.addActionListener(this);c.add(stop);c.addSeparator();// 设置一个菜单栏MenuBar mb = new MenuBar();mb.add(f);mb.add(c);mb.add(l);// 将构造完成的菜单栏交给当前程序的窗口; setMenuBar(mb); }// 动作时间响应成员;捕捉发送到本对象的各种事件; public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString cufile, selectfile, currentDirectory;if (e.getActionmand().equals("退出")) {// 调用dispose以便执行windowCloseddispose();return; }// 此事表明拥护选择了“播放”命令;// 如果当前有一个文件可以播放则执行播放命令;if (e.getActionmand().equals("播放")) {if (player != null) {player.start(); }return; }// 如果当前正在播放*一文件,则执行暂停;if (e.getActionmand().equals("暂停")) {if (player != null) {player.stop(); }return; } // 停止命令的响应;if (e.getActionmand().equals("停止")) {if (player != null) {player.stop();player.setMediaTime(new Time(0)); }return; } // 用户选择要播放的媒体文件if (e.getActionmand().equals("打开")) {FileDialog fd = new FileDialog(this, "打开媒体文件", FileDialog.LOAD); // fd.setDirectory(currentDirectory);fd.setVisible(true);// 如果用户放弃选择文件,则返回if (fd.getFile() == null) {return; }// 保存了所选文件的名称及其路径名称已被稍后使用// 同时设置当前文件夹路径selectfile = fd.getFile();currentDirectory = fd.getDirectory();cufile = currentDirectory + selectfile;// 将用户选择的文件作为一个菜单项加入播放列表,该菜单项名为该文件名; // 被点击后给出的命令串是该文件的全路径名MenuItem mi = new MenuItem(selectfile);mi.setActionmand(cufile);MenuBar mb = getMenuBar();Menu m = mb.getMenu(2);mi.addActionListener(this);m.add(mi);} else {// 程序逻辑运行到次表示用户选择了一个“播放列表”中的媒体文件// 此时可以通过如下动作获得该文件的全路径名cufile = e.getActionmand();selectfile = cufile; }// 如果存在一个播放器,则先将其关闭,稍后再重新创建// 创建播放器时需要捕捉一些异常if (player != null) {player.close(); }try {player = Manager.createPlayer(new MediaLocator("file:" + cufile));} catch (E*ception e2) {System.out.println(e2);return; }/** catch(NoPlayerE*ception e2){ System.out.println("不能找到播放器"); * return ; }*/if (player == null) {System.out.println("无法创建播放器");return; }first = false;setTitle(selectfile);// 设置处理播放控制器实际的对象;/**/player.addControllerListener(this);player.prefetch(); }// 菜单状态改变事件的响应函数;public void itemStateChanged(ItemEvent arg0) {// TODO Auto-generated method stub }public static void main(String[] args) {// TODO Auto-generated method stubnew MediaPlayer("播放器"); }// 调用绘图函数进行界面的绘制// public void update() { // }// 绘图函数成员//public void paint(Graphics g) { // }public void controllerUpdate(ControllerEvent e) {// TODO Auto-generated method stubContainer tainer = getContentPane();// 调用player.close()时ControllerClosedEvent事件出现// 如果存在视觉部件,则该部件应该拆除(为了一致起见,我们对控制面版部件也执行同样的操作,下一次需要时再构造)if (e instanceof ControllerClosedEvent) {if (vc != null) {remove(vc);vc = null; }if (cc != null) {remove(cc);cc = null; } }// 播放结束时,将播放指针置于文件之首,如果设定了循环播放,则再次启动播放器;if (e instanceof EndOfMediaEvent) {player.setMediaTime(new Time(0));if (loop) {player.start(); }return; }// PrefetchpletEvent事件发生后调用start,正式启动播放if (e instanceof PrefetchpleteEvent) {player.start();return; }// 本事件表示由于播放的资源已经确定;此时要将媒体的图形conmopnent// 如果有显示出来,同时将播放器player的控制显示到窗口里;if (e instanceof RealizepleteEvent) {// 如果媒体中有图像,将对应图像ponent载入窗体;vc = player.getVisualponent();if (vc != null)tainer.add(vc, BorderLayout.CENTER);// 将对应控制器ponent载入窗体;cc = player.getControlPanelponent();cc.setBackground(Color.blue);if (cc != null)tainer.add(cc, BorderLayout.SOUTH);// 有一些特殊媒体在播放时提供另外的控制手段,将控制器一并加入窗口; /* * gc=player.getGainControl(); gcc=gc.getControlponent();* if(gcc!=null) tainer.add(gcc,BorderLayout.NORTH); */// 根据媒体文件中是否有图像,设定相应的窗口大小if (vc != null) {pack();return;} else {setSize(300, 75);setVisible(true);return; } } } }。

基于java的音乐播放器课程设计报告 (1)

基于java的音乐播放器课程设计报告 (1)

Java课程设计报告题目:java音乐播放器姓名学号院系班级专业任课教师1.设计内容及要求能够实现简单的音乐播放器功能,如:打开本地文件,播放,暂停,停止,背景播放,单曲循环等等,界面充实,交互友好,可以添加多首歌曲目录一个简单的基于java语言的音乐播放器,菜单栏:文件,播放控制和播放列表,文件菜单包含打开、循环和退出三个菜单项;播放控制菜单包含播放、暂停和停止三个菜单项,播放列表菜单则是当向里面添加歌曲之后才能够查看到里面所包含的歌曲目录。

当播放歌曲时,可以调节音量大小或者设置是否静音;可以查看当前播放音乐的相关属性,如:歌曲名,歌手,发布时间,所属专辑等等。

2.需求分析1、时间跟踪条:根据歌曲播放的进度,控制条自动进行追踪,用户可以随时了解歌曲的播放进度2、音量控制键:可以点击该控键进行音量大小的调节以及是否静音的设置3、属性查询键:通过该控键,用户可以查看当前播放音乐的相属性,获取关于该歌曲的用户所需的相关信息。

4、菜单栏:①文件:打开:用户通过该菜单项打开本地音乐文件,听任何用户想听的歌曲循环:通过循环设置,用户可以重复的播放一首喜欢的歌曲退出:退出音乐播放器,及退出该程序②播放控制:播放:选择歌曲之后,通过点击播放菜单项启动播放器播放音乐暂停:暂停当前正在播放的音乐停止:停止当前正在播放的音乐③播放列表:当用户通过文件打开本地音乐文件后,及相当于在播放列表里面添加了歌曲,及形成相应的歌曲播放目录3.总体设计结合需求分析来看,整个设计包含了简单的菜单栏,根据用户需求设置了三个简单的实现基本操作的菜单,分别是:文件,播放控制,播放列表;根据具体的操作每个菜单还包含了相应的菜单项,文件:打开,循环,退出;播放控制:播放,暂停,停止;播放列表则没有菜单项,该菜单就是用来显示或者查看相应的歌曲目录的。

该音乐播放器设置了两个窗口,运行程序时打开的初始化欢迎界面,用户可以根据自己的需求进行菜单栏里面的基本操作和相应的设置,当用户添加或者打开音乐播放文件的时候,程序将会显示出第二个音乐文件播放窗口,与此同时,第一个初始化窗口将会自动隐藏,在音乐播放窗口里面用户可以进行相关的音乐播放的控制操作。

java播放mp3oggapeflac音乐

java播放mp3oggapeflac音乐

java播放mp3oggapeflac音乐java播放mp3/ogg/ape/flac音乐博客分类:•游戏音乐好吧,又要说一句了,java真是无所不能。

用java sound api播放wav, aiff, au, snd文件想必大家都已经知道了,这些格式是jdk原生支持的。

本文将介绍如何播放mp3/ogg/ape/flac音乐。

其实都是批发来的,我只是搬运工Σ( ° △ °|||)︴1. 先上代码Java代码1.import java.io.File;2.import java.io.IOException;3.4.import javax.sound.sampled.AudioFormat;5.import javax.sound.sampled.AudioInputStream;6.import javax.sound.sampled.AudioSystem;7.import javax.sound.sampled.DataLine;8.import javax.sound.sampled.SourceDataLine;9.10.public class AudioFilePlayer {11.12.public static void main(String[] args) {13.final AudioFilePlayer player = new AudioFilePlayer ();14.player.play("D:/xiaomi.ogg");15.//player.play("D:/01 - 謎解きはディナーのあとで.mp3");16.//player.play("D:/周杰伦-青花瓷.APE");17.//player.play("D:/周杰伦 - 听妈妈的话.flac");18.}19.20.public void play(String filePath) {21.final File file = new File(filePath);22.23.try {24.final AudioInputStream in = AudioSystem.getAudioIn putStream(file);25.26.final AudioFormat outFormat = getOutFormat(in.getF ormat());27.final info = new (SourceDat aLine.class, outFormat);28.29.final SourceDataLine line = (SourceDataLine) AudioSys tem.getLine(info);30.31.if (line != null) {32.line.open(outFormat);33.line.start();34.stream(AudioSystem.getAudioInputStream(outFormat, in), line);35.line.drain();36.line.stop();37.}38.39.} catch (Exception e) {40.throw new IllegalStateException(e);41.}42.}43.44.private AudioFormat getOutFormat(AudioFormat inFo rmat) {45.final int ch = inFormat.getChannels();46.final float rate = inFormat.getSampleRate();47.return new AudioFormat(AudioFormat.Encoding.PCM_ SIGNED, rate, 16, ch, ch * 2, rate, false);48.}49.50.private void stream(AudioInputStream in, SourceDataL ine line)51.throws IOException {52.final byte[] buffer = new byte[65536];53.for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {54.line.write(buffer, 0, n);55.}56.}57.}你没有看错,代码其实很简单,就是普通的java sound api,和原来播放wav, aiff, au, snd没什么两样。

音乐播放器Java课程设计报告

音乐播放器Java课程设计报告

《JA V A程序设计》课程设计报告题目:音乐播放器学院:计算机科学与工程学院专业:信息管理与信息系统学生姓名:杨柏昌学号:1400320228指导老师:汪华登目录1. 概要设计....................................................................2. 详细设计....................................................................3. 测试............................................................................4. 总结............................................................................概要设计播放器主要使用JMF框架实现,播放器界面如下详细设计package design.curriculum.JAVA;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.Border;import java.io.*;import javax.media.*;public class JMFPlayer extends JFrame implements ActionListener,ControllerListener{Player player=null;//声明一个Player对象String filePath;//存放音乐文件夹所在目录String fileName;//存放歌曲名称boolean click=false;//用来标记歌曲是否被点击int songIndex;//歌曲元素在列表中的位置int loop=3;//作为播放模式的标记JLabel songNameLable;JRadioButton sequenceBtn;JRadioButton singleBtn;JRadioButton randomBtn;ButtonGroup loopGroup;JButton beforeBtn;JButton nextBtn;JButton playBtn;JButton pauseBtn;JButton stopBtn;JButton fileBtn;JButton exitBtn;Component controlComponent;//player的控制面板Container container;JPanel songNamejp;JPanel loopjp;JPanel btnjp;JPanel listjp;JPanel btn2jp;List list;public JMFPlayer(){super("音乐播放器");setBounds(480,50,400,600);setResizable(false);//设置界面不可修改setVisible(true);container=getContentPane();//获得容器主面板container.setLayout(new BoxLayout(container,BoxLayout.Y_AXIS));songNamejp=new JPanel(new GridLayout(2,2));songNamejp.setSize(400,50);container.add(songNamejp);songNameLable=new JLabel("正在播放:未选取歌曲");songNamejp.add(songNameLable);songNamejp.setBorder(BorderFactory.createEtchedBorder());loopjp=new JPanel();loopjp.setSize(400,50);container.add(loopjp);sequenceBtn=new JRadioButton("顺序播放");sequenceBtn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent evt){loop = 1;}});singleBtn=new JRadioButton("单曲循环");singleBtn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent evt){loop = 0;}});randomBtn=new JRadioButton("随机播放");randomBtn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent evt){loop = 2;}});loopGroup=new ButtonGroup();loopGroup.add(sequenceBtn);loopGroup.add(singleBtn);loopGroup.add(randomBtn);loopjp.add(sequenceBtn);loopjp.add(singleBtn);loopjp.add(randomBtn);btnjp=new JPanel();btnjp.setLayout(new BoxLayout(btnjp,BoxLayout.X_AXIS));btnjp.setSize(400,50);container.add(btnjp);beforeBtn=new JButton("上一首");playBtn=new JButton("播放");pauseBtn=new JButton("暂停");stopBtn=new JButton("停止");nextBtn=new JButton("下一首");beforeBtn.addActionListener(this);playBtn.addActionListener(this);pauseBtn.addActionListener(this);stopBtn.addActionListener(this);nextBtn.addActionListener(this);btnjp.add(beforeBtn);btnjp.add(playBtn);btnjp.add(pauseBtn);btnjp.add(stopBtn);btnjp.add(nextBtn);listjp=new JPanel(new BorderLayout());listjp.setSize(300,300);container.add(listjp);Border border = BorderFactory.createEtchedBorder();Border titled = BorderFactory.createTitledBorder(border, "歌曲列表");listjp.setBorder(titled);fileBtn=new JButton("选择文件夹");exitBtn=new JButton("退出");fileBtn.addActionListener(this);exitBtn.addActionListener(this);btn2jp=new JPanel();btnjp.setSize(300,50);container.add(btn2jp);btn2jp.add(fileBtn);btn2jp.add(exitBtn);list=new List();list.setBackground(Color.pink);JScrollPane songListScroll = new JScrollPane(list);listjp.add(songListScroll, BorderLayout.CENTER);list.addMouseListener(new MouseAdapter() {//对歌曲点击的监听public void mouseClicked(MouseEvent e) {if (e.getClickCount() == 1) {fileName = list.getSelectedItem();songIndex=list.getSelectedIndex();click=true;}}});addWindowListener (new WindowAdapter(){public void windowClosing(WindowEvent e){if(player!=null){player.stop();player.deallocate();}System.exit(0);}});validate();}/** 对按钮事件的处理*/public void actionPerformed(ActionEvent e){if(e.getSource()==fileBtn){FileDialog dialog=new FileDialog(this,"音乐",FileDialog.LOAD);dialog.setVisible(true);filePath = dialog.getDirectory();if (filePath != null) {File filedir = new File(filePath);File[] filelist = filedir.listFiles();for (File file : filelist) {String filename = file.getName().toLowerCase();if(filename.endsWith(".wav")||filename.endsWith(".mp3")) {list.add(filename);}}}}if(e.getSource()==playBtn){if(click){click=!click;try{if(player!=null){player.stop();player.deallocate();}if(controlComponent!=null){songNamejp.remove(controlComponent);songNamejp.validate();}player=Manager.createPlayer(newMediaLocator("file:"+filePath+fileName));player.getDuration();player.prefetch();player.addControllerListener(this);songNameLable.setText(fileName);}catch (IOException e3) {}catch(NoPlayerException e2) {}}else if(!click){player.start();}}if(e.getSource()==exitBtn){System.exit(0);}if(e.getSource()==pauseBtn) {if(player!=null){player.stop();}}if(e.getSource()==stopBtn){if(player!=null){player.stop();player.setMediaTime(new Time(0));}elsereturn;}if(e.getSource()==beforeBtn){int n=list.getItemCount();if(songIndex<n){songIndex=songIndex-1;if(songIndex<0){songIndex=n-1;}fileName=list.getItem(songIndex);run();}}if(e.getSource()==nextBtn){int n=list.getItemCount();if(songIndex<n){songIndex=songIndex+1;if(songIndex>=n){songIndex=0;}fileName=list.getItem(songIndex);run();}}}public void run(){try{if(player!=null){player.stop();player.deallocate();}if(controlComponent!=null){songNamejp.remove(controlComponent);songNamejp.validate();}player=Manager.createPlayer(newMediaLocator("file:"+filePath+fileName));player.getDuration();player.prefetch();player.addControllerListener(this);songNameLable.setText(fileName);}catch (IOException e3) {}catch(NoPlayerException e2) {}}/** 对控制面板事件的处理* */public synchronized void controllerUpdate(ControllerEvent event){if(event instanceof RealizeCompleteEvent){if((controlComponent=player.getControlPanelComponent())!=null)songNamejp.add(controlComponent);}else if(event instanceof PrefetchCompleteEvent){player.start();}else if(event instanceof EndOfMediaEvent){player.setMediaTime(new Time(0));if(loop==0){player.start();}if(loop==1){int n=list.getItemCount();if(songIndex<n){fileName=list.getItem(songIndex+1);songIndex=songIndex+1;run();}}if(loop==2){int n=list.getItemCount();if(songIndex<n){songIndex=(int)(Math.random()*1000)%n;fileName=list.getItem(songIndex);run();}}}validate();}/** 主程序*/public static void main(String srgs[]){new JMFPlayer();}}测试1.运行程序如下:2.点击文件夹按钮弹出文件对话框,选择音乐文件夹界面如下:3.点击一首歌曲并播放总结做这个播放器用了两周的时间,一开始去图书馆借了好些书,发现了好几种类型做播放器用的API,发现有些比较繁杂和功能性不强,最终选用了JMF。

java 游戏开发学习情境5音乐播放器设计

java 游戏开发学习情境5音乐播放器设计
Java游戏开发 学习情境5:音乐播放器设计
08游戏3-2
目标:
◦ 为游戏添加音效,能够用Java加载、播放游戏中的声 音文件,并按要求控制播放。
任务:
◦ 用Java设计一个播放多种声音的程序。为电梯模拟游 戏添加背景音乐。
目标与仸务
学习过程
教师 项目核心 提供: 声音播放案例 提供: 多个声音列表 播放关键代码 提供: 电梯游戏的 声音和界面 学生 模仿完成: 声音播放案例 协作完成: 多个声音列表 播放 协作完成: 电梯游戏的 音效处理
音文件的例子,运行结果如图所示:
步骤一:创建一个Applet小程序

(1)通过继承Applet类来创建一个有三个 按钮的容器 import java.applet.*; public class mAudio extends Applet{} (2)定义成员属性 包含3个成员属性,分别表示播放的声音的 URL相对地址,当前播放的音频流和声音播 放状态。
应用升级

(2)编写一个类,通过播放AudioClip对象 来产生声音。(学生自主完成)
功能扩展之案例二





// 控制播放停止的按钮 private Button playClip; private Button loopClip; private Button stopClip; private Button stopAllClips; // 跟踪当前哪些音频片段在播放 private boolean[] clipsPlaying;
应用升级
问题陈述: 在电梯系统的模拟程序里,公司要求在模拟中 加入声音。例如,当一个人走路时,应能听到 脚步声。每当按下戒者复位一个楼层按钮戒者 电梯按钮时,应当听到单击声。电梯到达时铃 声应当响起,且在门打开时戒者关上时,门应 当吱吱作响。最后,当电梯在楼层间运行时, 应播放“电梯音乐”。

Java实战音乐播放器

Java实战音乐播放器

Java实战音乐播放器-播放音乐文件,包括基本的音频控制创建一个基本的音乐播放器是一个有趣而复杂的项目,它涉及到音频文件的处理、用户界面的设计以及音频控制功能的实现。

在这个实战博客中,我将向您展示如何使用Java来创建一个简单的音乐播放器。

我们将使用Java的Swing库来构建用户界面,以及Java的javax.sound.sampled库来处理音频文件和音频控制。

这个播放器将能够加载和播放本地音频文件,包括基本的音频控制功能,如播放、暂停、停止和音量调节。

步骤1:创建Java项目首先,创建一个新的Java项目,并添加Swing和javax.sound.sampled库的依赖。

您可以使用Eclipse、IntelliJ IDEA或任何其他Java集成开发环境来完成这些任务。

步骤2:创建用户界面在我们的音乐播放器中,我们需要一个用户界面来加载和控制音乐文件。

我们将使用Swing 来创建以下基本组件:文件选择器:用于选择要播放的音乐文件。

播放按钮:用于开始或继续播放音乐。

暂停按钮:用于暂停音乐的播放。

停止按钮:用于停止音乐的播放。

音量控制:用于调整音量。

下面是一个示例的用户界面代码:javaCopy codeimport javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class MusicPlayerGUI extends JFrame {private JButton playButton;private JButton pauseButton;private JButton stopButton;private JSlider volumeSlider;private JFileChooser fileChooser;public MusicPlayerGUI() {setTitle("音乐播放器");setSize(400, 200);setDefaultCloseOperation(EXIT_ON_CLOSE);setLayout(new FlowLayout());fileChooser = new JFileChooser();playButton = new JButton("播放");pauseButton = new JButton("暂停");stopButton = new JButton("停止");volumeSlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);add(fileChooser);add(playButton);add(pauseButton);add(stopButton);add(volumeSlider);playButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 实现播放音乐的逻辑}});pauseButton.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {// 实现暂停音乐的逻辑}});stopButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 实现停止音乐的逻辑}});volumeSlider.addChangeListener(new ChangeListener() { @Overridepublic void stateChanged(ChangeEvent e) {// 实现音量调节的逻辑}});}public static void main(String[] args) {SwingUtilities.invokeLater(new Runnable() {@Overridepublic void run() {new MusicPlayerGUI().setVisible(true);}});}}步骤3:音频处理和控制接下来,我们需要添加音频处理和控制的逻辑。

java简易音乐播放器

java简易音乐播放器

package com.test.audio;import java.io.File;import java.awt.BorderLayout;import java.awt.FileDialog;import java.awt.Frame;import java.awt.GridLayout;import bel;import java.awt.List;import java.awt.Menu;import java.awt.MenuBar;import java.awt.MenuItem;import java.awt.MenuShortcut;import java.awt.Panel;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem;import javax.sound.sampled.DataLine;import javax.sound.sampled.SourceDataLine; public class MusicPlayer extends Frame {private static final long serialVersionUID = 1L;boolean isStop = true;// 控制播放线程boolean hasStop = true;// 播放线程状态String filepath;// 播放文件目录String filename;// 播放文件名称AudioInputStream audioInputStream;// 文件流AudioFormat audioFormat;// 文件格式SourceDataLine sourceDataLine;// 输出设备List list;// 文件列表Label labelfilepath;//播放目录显示标签Label labelfilename;//播放文件显示标签public MusicPlayer() {// 设置窗体属性setLayout(new BorderLayout());setTitle("D哥音乐播放器");setSize(350, 370);// 建立菜单栏MenuBar menubar = new MenuBar();Menu menufile = new Menu("文件");MenuItem menuopen = new MenuItem("打开", new MenuShortcut(KeyEvent.VK_O));menufile.add(menuopen);menufile.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {open();}});menubar.add(menufile);setMenuBar(menubar);// 文件列表list = new List(10);list.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {// 双击时处理if (e.getClickCount() == 2) {// 播放选中的文件filename = list.getSelectedItem();play();}}});add(list, "Center");// 信息显示Panel panel = new Panel(new GridLayout(2, 1));labelfilepath = new Label("播放目录:");labelfilename = new Label("播放文件:");panel.add(labelfilepath);panel.add(labelfilename);add(panel, "North");// 注册窗体关闭事件addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});setVisible(true);}// 打开private void open() {FileDialog dialog = new FileDialog(this, "Open", 0);dialog.setVisible(true);filepath = dialog.getDirectory();if (filepath != null) {labelfilepath.setText("播放目录:" + filepath);// 显示文件列表list.removeAll();File filedir = new File(filepath);File[] filelist = filedir.listFiles();for (File file : filelist) {String filename = file.getName().toLowerCase();if (filename.endsWith(".mp3") || filename.endsWith(".wav")) {list.add(filename);}}}}// 播放private void play() {try {isStop = true;// 停止播放线程// 等待播放线程停止System.out.print("开始播放:" + filename);while (!hasStop) {System.out.print(".");try {Thread.sleep(10);} catch (Exception e) {}}System.out.println("");File file = new File(filepath + filename);labelfilename.setText("播放文件:" + filename);// 取得文件输入流audioInputStream = AudioSystem.getAudioInputStream(file);audioFormat = audioInputStream.getFormat();// 转换mp3文件编码if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,audioFormat.getSampleRate(), 16, audioFormat.getChannels(), audioFormat.getChannels() * 2,audioFormat.getSampleRate(), false);audioInputStream = AudioSystem.getAudioInputStream(audioFormat,audioInputStream);}// 打开输出设备 dataLineInfo = new (SourceDataLine.class, audioFormat,AudioSystem.NOT_SPECIFIED);sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);sourceDataLine.open(audioFormat);sourceDataLine.start();// 创建独立线程进行播放isStop = false;Thread playThread = new Thread(new PlayThread());playThread.start();} catch (Exception e) {e.printStackTrace();}}public static void main(String args[]) {new MusicPlayer();}// 播放线程class PlayThread extends Thread {byte tempBuffer[] = new byte[320];public void run() {try {int cnt;hasStop = false;// 读取数据到缓存数据while ((cnt = audioInputStream.read(tempBuffer, 0,tempBuffer.length)) != -1) {if (isStop)break;if (cnt > 0) {// 写入缓存数据sourceDataLine.write(tempBuffer, 0, cnt);}}// Block等待临时数据被输出为空sourceDataLine.drain();sourceDataLine.close();hasStop = true;} catch (Exception e) {e.printStackTrace();System.exit(0);}}}}。

最新一个简单的音乐播放器的java代码

最新一个简单的音乐播放器的java代码

import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.*;import javax.swing.*;import javax.media.bean.playerbean.*;import java.io.IOException;import javax.swing.JOptionPane;import java.awt.*;public class lyt extends JFrame implements Runnable { public MediaPlayer soumd1;private JButton playSound, stopsound, tjian,loopSound; private JMenuItem jmiOpen,jmiSave,jmiExit,jmiAbout; public JPanel jp1, jp2, jp3, jp4;public JList jl;private JButton last, next, stop;public Vector vector, mingcheng;boolean fo = false, geshi = false;JLabel jl1, jl2, sj1, sj2;JTextField jt1, jt2;int a = 0, b = 0, you = 1, mm = 0;public lyt() {super("200842018java音乐播放器");soumd1 = new MediaPlayer();Container c = getContentPane();c.setLayout(new FlowLayout());mingcheng = new Vector();jp1 = new JPanel();c.add(jp1);JMenuBar mb=new JMenuBar();setJMenuBar(mb);JMenu fileMenu=new JMenu("文件");mb.add(fileMenu);JMenu helpMenu=new JMenu("帮助");mb.add(helpMenu);fileMenu.add(jmiOpen=new JMenuItem("打开"));fileMenu.add(jmiSave=new JMenuItem("播放")); helpMenu.add(jmiAbout=new JMenuItem("关于"));JPanel jp2 = new JPanel();jp2.setLayout(new FlowLayout());jp2.add(last = new JButton("后退"));jp2.add(playSound = new JButton("播放"));jp2.add(tjian = new JButton("添加"));jp2.add(next = new JButton("前进"));jp2.add(stopsound = new JButton("停止"));jp2.add(loopSound= new JButton("循环"));getContentPane().add(jp2, BorderLayout.SOUTH);jp2 = new JPanel();jp2.add(playSound);jp2.add(stopsound);c.add(jp2);jp4 = new JPanel();sj1 = new JLabel();sj2 = new JLabel();jp4.add(sj1);jp4.add(sj2);c.add(jp4);vector = new Vector();jl = new JList(mingcheng);jl.setVisibleRowCount(4);jl.setFixedCellHeight(30);jl.setFixedCellWidth(265);jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION) ;c.add(new JScrollPane(jl));jp3 = new JPanel();jp3.add(tjian);c.add(jp3);try {String s = "d:\\music.txt";ObjectInputStream input = new ObjectInputStream( new FileInputStream(s));lujin a1 = (lujin) input.readObject();mingcheng = a1.b;vector = a1.a;jl.setListData(mingcheng);}catch (Exception e) {System.out.println("D盘没有保存音乐文件");}jmiOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) {JFileChooser fileChooser = new JFileChooser();fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );fileChooser.setCurrentDirectory(new File("."));fileChooser.setFileFilter(newjavax.swing.filechooser.FileFilter() {public boolean accept(File file) {String name = file.getName().toLowerCase();return name.endsWith(".mp3")|| file.isDirectory();}public String getDescription() {return "音乐文件(*.mp3)";}});if (fileChooser.showOpenDialog(lyt.this) == JFileChooser.APPROVE_OPTION) {String fileName =fileChooser.getSelectedFile().getAbsolutePath();vector.add(fileName);StringBuffer buffer = daoxu(fileName);mingcheng.add(buffer);}}});tjian.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) {JFileChooser fileChooser = new JFileChooser();fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );fileChooser.setCurrentDirectory(newFile("."));fileChooser.setFileFilter(newjavax.swing.filechooser.FileFilter() {public boolean accept(File file) {String name =file.getName().toLowerCase();return name.endsWith(".mp3")|| file.isDirectory();}public String getDescription() {return "音乐文件(*.mp3)";}});if (fileChooser.showOpenDialog(lyt.this) == JFileChooser.APPROVE_OPTION) {String fileName =fileChooser.getSelectedFile().getAbsolutePath();vector.add(fileName);StringBuffer buffer = daoxu(fileName);mingcheng.add(buffer);}}});jmiSave.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (jl.getSelectedIndex() >= 0) {String yy = (String)vector.get(jl.getSelectedIndex());File ff = new File(yy);if (ff.exists()) {if (yy.matches("[\\S\\s]*.mp3") || yy.matches("[\\S\\s]*.mp3")) {if (soumd1 != null) {a = 0;b = 0;you = 0;soumd1.stop();}soumd1.setMediaLocation("file:/" + yy);fo = true;soumd1.start();geshi = true;try {Thread.sleep(500);}catch (InterruptedException eee) {}}elseJOptionPane.showMessageDialog(null,"无法播放");}elseJOptionPane.showMessageDialog(null,"此歌曲已经存在");}elseJOptionPane.showMessageDialog(null, "请选择音乐文件");}});loopSound.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (jl.getSelectedIndex() >= 0) {String yy = (String) vector.get(jl.getSelectedIndex());File ff = new File(yy);if (ff.exists()) {if (yy.matches("[\\S\\s]*.mp3")|| yy.matches("[\\S\\s]*.MP3")) {if (soumd1 != null) {a = 0;b = 0;you = 0;soumd1.stop();}soumd1.setMediaLocation("file:/" + yy);fo = true;soumd1.start();geshi=true;try {Thread.sleep(500);} catch (InterruptedException we) {}} elseJOptionPane.showMessageDialog(null,"不支持此格式,无法播放");} elseJOptionPane.showMessageDialog(null,"此歌曲文件已经存在");}elseJOptionPane.showMessageDialog(null, "请选择音乐文件");}});playSound.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (jl.getSelectedIndex() >= 0) {String yy = (String)vector.get(jl.getSelectedIndex());File ff = new File(yy);if (ff.exists()) {if (yy.matches("[\\S\\s]*.mp3") ||yy.matches("[\\S\\s]*.mp3")) {if (soumd1 != null) {a = 0;b = 0;you = 0;soumd1.stop();}soumd1.setMediaLocation("file:/" + yy);fo = true;soumd1.start();geshi = true;try {Thread.sleep(500);}catch (InterruptedException eee) {}}elseJOptionPane.showMessageDialog(null,"无法播放");}elseJOptionPane.showMessageDialog(null,"此歌曲已经存在");}elseJOptionPane.showMessageDialog(null, "请选择音乐文件");}});stopsound.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {a = 0;b = 0;you = 0;mm = 0;geshi = false;fo = false;sj1.setText(null);sj2.setText(null);if (jl.getSelectedIndex() >= 0)soumd1.stop();}});jl.addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent event) {if (event.getClickCount() == 2) {if (jl.getSelectedIndex() >= 0) {String yy = (String)vector.get(jl.getSelectedIndex());File ff = new File(yy);if (ff.exists()) {if (yy.matches("[\\S\\s]*.mp3") ||yy.matches("[\\S\\s]*.mp3")) {if (soumd1 != null) {a = 0;b = 0;you = 0;soumd1.stop();}soumd1.setMediaLocation("file:/" + yy);fo = true;soumd1.start();geshi = true;try {Thread.sleep(500);}catch (InterruptedException e) {}}elseJOptionPane.showMessageDialog(null,"无法播放");}elseJOptionPane.showMessageDialog(null,"此歌曲已经存在");}}if (event.isMetaDown()) {if (jl.getSelectedIndex() >= 0) {int a = jl.getSelectedIndex();jt1.setText(mingcheng.get(jl.getSelectedIndex()) + "");jt2.setText(vector.get(jl.getSelectedIndex()) + "");}}}});addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {String s = "d:\\music.txt";mmm(vector, mingcheng, s);System.exit(0);}});setSize(370, 270);setVisible(true);}public void run() {while (true) {Date now = new Date();try {Thread.sleep(1000);}catch (InterruptedException e) {}if (geshi) {you = (int) soumd1.getMediaTime().getSeconds();mm = you % 60;if (you != 0)if (you % 60 == 0) {b++;}sj1.setText(b + ":" + mm);}}}public StringBuffer daoxu(String fileName) {String mc = "";for (int i = fileName.length(); i >= 1; i--) {if (fileName.charAt(i - 1) == '\\')break;mc += fileName.charAt(i - 1);}StringBuffer buffer = new StringBuffer(mc);StringBuffer mm = buffer.reverse();return mm;}public void mmm(Vector vector, Vector mingcheng, String lujin) {lujin a = new lujin(vector, mingcheng);try {ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(lujin));output.writeObject(a);output.flush();output.close();}catch (Exception e) {}}public static void main(String agrs[]) {lyt s = new lyt();Thread t1 = new Thread(s);t1.start();s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }class lujinimplements Serializable {Vector a = new Vector();Vector b = new Vector();public lujin(Vector vector, Vector mingcheng) {a = vector;b = mingcheng;}}}。

Java音乐播放器设计文献综述

Java音乐播放器设计文献综述

****大学专业文献综述题目:Java音乐播放器设计文献综述姓名:****学院:计算机科学与信息工程学院专业:计算机科学与技术班级:*********学号:****成绩:指导教师:职称:2012 年4月6 日****大学教务处制Java音乐播放器设计文献综述作者:**** 指导教师:摘要:随着科学技术的发展,人们对生活质量的要求越来越高,在视听享受方面,家庭影院越来越普遍,便携式电子设备也日趋成熟。

目前,人们对嵌入式媒体播放器的研究越来越广泛了,国内外已经出现了像MP3、MP4和智能手机等众多样式的便携式嵌入式媒体播放器。

但由于种种环境及条件的限制,这些便携式的媒体播放器都只能播放单一的或几种固定的媒体格式,可扩展性都比较差;而现在随着应用的不断增多,越来越多的更先进的压缩算法被提出,导致了媒体格式的多样化,在这种情况下,必然要求嵌入式媒体播放器要适应多种格式。

为此,通过对各种PC机上的播放器设计架构的研究与借鉴,在本文中主要在软件方面为嵌入式媒体播放器设计了一种可扩展性架构,并设计了播放器界面,实现了播放器的功能。

关键词:Java ;音乐;播放器;Designing the java player of musicAbstract:With the development of science and technology, people of the quality of life in demand is higher and higher, in the audio-visual enjoy, home theater is becoming more and more common, portable electronic equipment has become more and more mature. At present, the people of embedded media player is more and more extensive research, had appeared at home and abroad like MP3, MP4 and smart phones and many other styles of portable embedded media player. But because of various environmental and limitations, the portable media player can only play a single or several fixed media formats, scalability is poor. And now, with the increasing of the application, more and more of the more advanced compression algorithm was proposed, leading to the media format diversity, in this case, the inevitable requirement embedded media player to adapt to a variety of formats. Therefore, through various PC player design framework of research and the reference, in this paper mainly in software for embedded media player design a kind of scalability framework, and design the player interface, implements some functionality of the player.Key words: Java ;music;player;前言:随着科学技术的发展,嵌入式系统工业是专用计算机工业,其目的就是要把一切变得更简单、更方便、更普遍、更适用。

音乐播放器Java课程设计报告

音乐播放器Java课程设计报告

课程设计报告课程设计题目:java音乐播放器学生姓名:专业:XXXXXXXXXXXXX班级:指导教师:20XX年X月X日一、课程设计目的1、编程设计音乐播放软件,使之实现音乐播放的功能。

2、培养学生用程序解决实际问题的能力和兴趣。

3、加深java中对多媒体编程的应用。

二、课程设计的要求利用学到的编程知识和编程技巧,要求学生:1、系统设计要能完成题目所要求的功能,设计的软件可以进行简单的播放及其他基本功能。

2、编程简练,可用,尽可能的使系统的功能更加完善和全面3、说明书、流程图要清楚。

三、课程设计内容1、课程设计的题目及简介音乐播放软件要求:有图形界面,能播放MP3歌曲,有播放列表,前一首、后一首等常用播放软件功能。

2、设计说明主要运用多媒体编程、图形界面、数组及循环进行设计,从而实现简单的音乐播放。

public MyMusicPlayer():实现窗口的成员方法publi c void run():实现改变歌曲的播放状态的成员方法public AudioClip loadSound(String filename):实现对声音的加载public void mouseClicked(MouseEvent e):实现对按钮的监听public void itemStateChanged(ItemEvent arg0):返回一个AudioClip对象3、程序流程图4、程序清单import java.awt.*;import javax.swing.*;import java.applet.*;import java.awt.event.*;import .*;public class MyMusicPlayer extends Thread implements MouseListener,ItemListener{JFrame MainFrame=new JFrame("MyMusicPlayer"); //定义主窗体JLabel songname=new JLabel(); //用标签显示状态JButton last=new JButton();JButton play=new JButton();JButton next=new JButton();JButton loop=new JButton(); //定义上一曲,播放,下一曲,循环四个按钮JLabel list=new JLabel("播放列表");List songlist=new List(); //显示歌曲列表AudioClip[] song=new AudioClip[10]; //将音频文件放在数组中AudioClip playsong; //当前选定播放的音频文件String[] name={"黄小琥 - 顺其自然.wav","陈奕迅-张氏情歌.wav","黄小琥 _没那么简单.wav","戚薇-如果爱忘了.wav","郭采洁 - 狠狠哭.wav","test.wav"};//将所有文件名存放在字符串数组name中String playname; //当前选定播放的音频名int j=0; //记录当前选定播放的是哪首歌曲,默认为第一首boolean playbutton=true; //记录播放状态,默认为暂停播放boolean loopbutton=false; //记录循环状态,默认为没有循环Thread thread=new Thread("pl");static MyMusicPlayer Yu;public MyMusicPlayer(){MainFrame.setLayout(null);MainFrame.setBounds(300,50,310,500);MainFrame.setVisible(true);MainFrame.setDefaultCloseOperation(3);MainFrame.add(songname);Font sname=new Font("斜体",Font.ITALIC,18);songname.setFont(sname); //设置显示状态的字体为斜体songname.setText("我的音乐播放器");songname.setBounds(10,10,300,40);last.setBounds(10,70,50,40);play.setBounds(70,70,50,40); //设置四个功能键位置和大小next.setBounds(130,70,50,40);loop.setBounds(190,70,50,40);last.setIcon(new ImageIcon("1.png"));play.setIcon(new ImageIcon("2.png"));next.setIcon(new ImageIcon("3.png"));loop.setIcon(new ImageIcon("4.png")); //设置四个功能键的图片last.addMouseListener(this);play.addMouseListener(this);next.addMouseListener(this);loop.addMouseListener(this); //添加按键鼠标监听器MainFrame.add(last);MainFrame.add(play);MainFrame.add(next);MainFrame.add(loop);list.setBounds(10,120,100,30);Font lis=new Font("宋",Font.BOLD,15);list.setFont(lis); //显示“播放列表”MainFrame.add(list);songlist.setBounds(10,150,250,300);songlist.setBackground(Color.GREEN); //设置播放列表的背景色为绿色songlist.setVisible(true);songlist.addItemListener((ItemListener) this); //添加列表监听器MainFrame.add(songlist);for(int i=0;i<name.length;i++){song[i]=loadSound(name[i]); //逐个获取音频文件songlist.add(name[i]); //将歌曲名添加到播放列表}playsong=song[0];}public static void main(String[] args){Yu=new MyMusicPlayer();Yu.start();}public void mouseClicked(MouseEvent e) {JButton btn=(JButton)e.getSource();playsong.stop();if(btn==play){ if(playbutton==false)playbutton=true;elseplaybutton=false; //当按下play后改变播放状态}elseif(btn==last){ j-=1; //当按下last后将上一曲选定if(j<0)j=name.length-1; //若之前选定为第一首,则跳到最后一首playbutton=true;loopbutton=false;}elseif(btn==next){ j+=1; //当按下next后将下一曲选定if(j>=name.length)j=0; //若之前选定为最后一首,则跳到第一首playbutton=true;loopbutton=false;}elseif(btn==loop){ if(loopbutton==false){loopbutton=true;playbutton=true;}else{ //按下loop后,改变循环状态和播放状态loopbutton=false;playbutton=false;}}if(playbutton==true)Yu.run();else{songname.setText("暂停播放:"+playname); //暂停播放歌曲play.setIcon(new ImageIcon("2.png"));}}public void mouseEntered(MouseEvent arg0) {// TODO Auto-generated method stub}public void mouseExited(MouseEvent arg0) {// TODO Auto-generated method stub}public void mousePressed(MouseEvent arg0) {// TODO Auto-generated method stub}public void mouseReleased(MouseEvent arg0) {// TODO Auto-generated method stub}public void itemStateChanged(ItemEvent arg0) {String str=songlist.getSelectedItem();playsong.stop();for(int i=0;i<name.length;i++)if(str==name[i]){j=i;break;}Yu.run();}public void run(){playsong=song[j]; //播放状态为播放时playsong.play(); //播放选定歌曲playname=name[j];if(loopbutton==true){playsong.loop(); //循环播放选定歌曲songname.setText("循环播放:"+playname);}elsesongname.setText("正在播放:"+playname);songlist.select(j); //播放列表中选定播放歌曲项目play.setIcon(new ImageIcon("5.png"));}public AudioClip loadSound(String filename){ //返回一个AudioClip对象URL url=null;try{url=new URL("file:"+filename);}catch(MalformedURLException e){ }return Applet.newAudioClip(url);}}5、程序运行结果四、课程设计总结经过这几天的Java课程设计,让我知道了做课程设计的一般方法,通过各种努力终于完成了设计要求.这次课程设计基本上含盖了大学一年学习到的Java 语言知识点,课设题目要求不仅要求对课本知识有较深刻的了解,同时要求程序设计者有较强的思维和动手能力。

Java综合实验-简易音乐播放器-实验报告

Java综合实验-简易音乐播放器-实验报告

Java综合实验实验报告一、实验要求根据下列描述:为某音乐爱好者开发音乐管理系统,该系统可以为音乐爱好者对已有的音乐信息检索、音乐播放、音乐收藏进行管理。

(1)音乐检索:对指定音乐名称进行检索,获取音乐的基本信息。

(2)音乐播放:对已有音源的音乐进行播放音乐收藏:对喜欢的音乐进行收藏管理,记录音源的位置等;二、核心思想利用MVC模式,结合javafx和SceneBuilder开发音乐管理系统三、实验原理四、实验环境Window 11、JDK-17、Intellij IDEA、JavaFX Scene Builder 2.0五、核心代码:1.业务逻辑:①User类package prehensiveExperiment.logic;import java.util.ArrayList;import java.util.List;/*** 用户类* 这个类记录了用户信息:账号、密码以及自己维护的音乐收藏*/public class User {private String account;private String password;private List<Music> musicCollection;public User(String account, String password) {this.account = account;this.password = password;musicCollection = new ArrayList<>();}public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}/*** 获取音乐收藏* @return musicCollection*/public List<Music> getMusicCollection() {return musicCollection;}/*** 收藏歌曲* @param music*/public void addMusic(Music music){musicCollection.add(music);}/*** 取消收藏* @param music*/public void deleteMusic(Music music){musicCollection.remove(music);}@Overridepublic String toString() {return "User{" +"account='" + account + '\'' +", password='" + password + '\'' +", musicCollection=" + musicCollection +'}';}}②Music类:package prehensiveExperiment.logic;/*** 音乐类* 这个类记录了歌曲的信息:歌曲名、歌手、歌曲时长、大小、发行时间、专辑*/public class Music implements Comparable<Music>{private String name;private String singer;private String totalTime;private String size;private String startTime;private String album;public Music(String name, String singer, String totalTime, String size, String startTime, String album) { = name;this.singer = singer;this.totalTime = totalTime;this.size = size;this.startTime = startTime;this.album = album;}public String getName() {return name;}public void setName(String name) { = name;}public String getSinger() {return singer;}public void setSinger(String singer) {this.singer = singer;}public String getTotalTime() {return totalTime;}public void setTotalTime(String totalTime) {this.totalTime = totalTime;}public String getSize() {return size;}public void setSize(String size) {this.size = size;}public String getStartTime() {return startTime;}public void setStartTime(String startTime) {this.startTime = startTime;}public String getAlbum() {return album;}public void setAlbum(String album) {this.album = album;}/*** 默认按歌手排序* @param o* @return*/@Overridepublic int compareTo(Music o){return pareTo(o.singer);}@Overridepublic String toString() {return "Music{" +"name='" + name + '\'' +", singer='" + singer + '\'' +", totalTime='" + totalTime + '\'' +", size='" + size + '\'' +", startTime='" + startTime + '\'' +", album='" + album + '\'' +'}';}}③MusicSys类:package prehensiveExperiment.logic;import java.io.File;import java.io.FileNotFoundException;import java.util.ArrayList;import parator;import java.util.List;import java.util.Scanner;/*** 音乐管理系统* 这个类维护了用户音乐收藏、本地音乐以及播放器的常规操作*/public class MusicSys {private Music currentMusic;private int index;//当前播放音乐的索引值private String state;private double volume;private List<User> users;//用户private int index2;//当前登录用户的索引值private List<Music> musics;//本地音乐private static MusicSys instance = null;//单例模式/*** 构造函数* 分别调用createUser()和createMusic()录入用户和音乐信息* 播放器初始状态:默认关闭(state = OFF),如果打开默认播放第一首歌(index=0),音量调至一半(volume = 0.5)* @throws FileNotFoundException*/private MusicSys() throws FileNotFoundException {users = new ArrayList<>();musics = new ArrayList<>();createUser();createMusic();index = 0;currentMusic = musics.get(index);state = "OFF";volume = 0.5;}/*** 单一工厂* @return instance* @throws FileNotFoundException*/public static MusicSys getInstance() throws FileNotFoundException {if(instance == null)instance = new MusicSys();return instance;}/*** 这个方法通过扫描事先写好的User.txt文件,将用户信息一条条录入。

java音乐播放器-乐乐音乐播放器

java音乐播放器-乐乐音乐播放器

java音乐播放器-乐乐音乐播放器示例图:111711_BEOg_437124.jpg111712_ZLs1_437124.jpgEnterProgram.java ~ 2KBpackage com.happy.enterProgram;import java.awt.Font;import java.util.Enumeration;import javax.swing.SwingUtilities;import javax.swing.SwingWorker;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException; import javax.swing.plaf.FontUIResource;import mon.Constants;import com.happy.manage.MediaManage;import com.happy.service.MediaPlayerService;import com.happy.ui.MainFrame;import com.happy.ui.SplashFrame;import com.happy.util.DataUtil;import com.happy.util.FontsUtil;public class EnterProgram {/*** 应用启动窗口*/private static SplashFrame splashFrame;/*** 主窗口*/private static MainFrame mainFrame;/*** 程序入口** @throws UnsupportedLookAndFeelException* @throws IllegalAccessException* @throws InstantiationException* @throws ClassNotFoundException**/public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,UnsupportedLookAndFeelException {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());SwingUtilities.invokeLater(new Runnable() {public void run() {splashFrame = new SplashFrame();splashFrame.setVisible(true);init();}});}protected static void init() {new SwingWorker<Void, Void>() {@Overrideprotected Void doInBackground() {initGlobalFont(FontsUtil.getBaseFont(Constants.APPFONTSIZE));// 先初始化数据DataUtil.init();// 初始化播放列表数据MediaManage.getMediaManage().initPlayListData(); // 初始化播放器服务MediaPlayerService.getMediaPlayerService().init(); SwingUtilities.invokeLater(new Runnable() {public void run() {mainFrame = new MainFrame();splashFrame.setVisible(false);mainFrame.setVisible(true);}});return null;}@Overrideprotected void done() {}}.execute();}/*** 统一设置字体,父界面设置之后,所有由父界面进入的子界面都不需要再次设置字体*/private static void initGlobalFont(Font font) {FontUIResource fontRes = new FontUIResource(font);for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {Object key = keys.nextElement();Object value = UIManager.get(key);if (value instanceof FontUIResource) {UIManager.put(key, fontRes);}}}}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
放器
系统需求分析规格说明书
二○一三年十一月
基于 java 的音乐播放器
目录
基于 java 的音乐播放器...................................................................................................................1 1 引言................................................................................................................................................. 3
3.1.1 音量控制..................................................................................................................6 3.1.2 静音控制..................................................................................................................7 3.1.3 上一首/下一首控制................................................................................................7 3.1.4 播放控制..................................................................................................................7 3.1.5 暂停控制..................................................................................................................8 3.1.6 播放文件控制..........................................................................................................8 3.1.7 播放歌曲文件控制..................................................................................................8 3.1.8 添加歌曲文件控制..................................................................................................9 3.1.9 删除歌曲文件控制..................................................................................................9 3.2 播放器友好性需求.............................................................................................................10 3.2.1 视觉外观显示........................................................................................................10 3.2.2 播放状态显示........................................................................................................10 3.3 歌词及背景显示................................................................................................................. 11 3.3.1 歌词显示................................................................................................................ 11 3.3.2 歌曲背景显示........................................................................................................11 4 系统类图....................................................................................................................................... 12 5 关键代码....................................................................................................................................... 13 5.1 Lrc 歌词处理...................................................................................................................... 13 5.2 歌词展示.............................................................................................................................13 6 程序截图....................................................................................................................................... 18 6.1 程序启动界面.....................................................................................................................18 6.2 显示脉冲界面.....................................................................................................................19 6.3 显示歌词界面.....................................................................................................................20 7 性能需求....................................................................................................................................... 20 7.1 数据精确度........................................................................................................................20 7.2 时间特性............................................................................................................................21 7.3 适应性................................................................................................................................ 21 8 运行需求....................................................................................................................................... 21 9 产品质量需求............................................................................................................................... 21 9.1 故障分析............................................................................................................................21 9.2 系统利用率........................................................................................................................22 9.3 可靠性................................................................................................................................ 22
相关文档
最新文档