基于java的音乐播放器的设计重点讲义_第1页
基于java的音乐播放器的设计重点讲义_第2页
基于java的音乐播放器的设计重点讲义_第3页
基于java的音乐播放器的设计重点讲义_第4页
基于java的音乐播放器的设计重点讲义_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、课 程 设 计 报 告课程名称:WEB课程设计20162017 学年度第一学期题 目: 基于java的音乐播放器 系 别: 信息工程学院 专 业: 计算机科学与技术 专业班级: 指导教师: 2 编者 目录 一.需求分析1(一)背景1(二)音乐播放器需求分析1二概要设计4(一)系统所用类及各类的主要说明4(二)系统UML类图5三.详细设计6(一)核心技术介绍6(二)创建播放器的图形用户界面所需的控件9四 .系统实现12五测试24结论26 一.需求分析(一)背景 随着计算机的广泛运用,手机市场的迅速发展,各种音频视频也在网上广为流传,这些资源看似平常,但以渐渐成为人们必不可少的 一部分了,于是各种

2、手机播放器也紧跟着发展起来,但是很多播放器一味追求外观花哨,功能庞大,对很多用户的时间造成了很多资源的浪费,比如CPU、内存的占有率过高,在用户需求多用户操作时,受到了不小的影响,带来了许多的不便,而对于大多数普通用户,许多功能用不上,形同虚设。(二)音乐播放器需求分析 根据项目,我们可以获得项目系统的基本需求主要是功能需求而系统的功能需求,我们分成三部分来概括,即播放器的基本控制需求、播放列表管理需求和性能需求。以下分别描述: 2 1、播放器的基本控制需求(1)播放:点击播放列表中的歌曲进行播放。(2)停止:在歌曲列表下方有一个小按钮,当歌曲正在播放时,显示的是可停止的按钮,点击则停止当前播

3、放的歌曲。(3)上一曲:歌曲在播放或者未播放时单击上一曲按钮时歌曲会跳到列表的当前歌曲的上一首。(4)下一曲:歌曲在播放或者未播放时单击下一曲按钮时歌曲会跳到列表的当前歌曲的下一首。(5)停止:单击停止按钮时立即停止播放当前的歌曲。 用户需求图 音乐播放器系统流程图2、播放列表管理(1)添加歌曲文件夹:在播放列表中单击,选择添加的文件夹则弹出一个添加文件夹的会话框,点击要添加的歌曲,则弹出“添加成功”的消息提示,否则弹出“添加失败”的相关信息。(2)添加歌曲: 选中歌曲后,单击选择添加歌曲或者点击左下角的添加按钮则弹出一个添加歌曲信息的会话框,点击提交则进行添加,添加成功则更新了歌曲

4、列表。3、性能需求(1)当要求歌曲播放时,软件响应时间最长不超过2秒(2)当要求歌曲暂停时,软件响应时间最长不超过1秒系统能持续运行,不影响其他程序的使用,不多占用内存,不会造成死机等问题。二概要设计(一)系统所用类及各类的主要说明(1)界面样式MUSICPLAYER类:主类,作为框架从指定的类获取图像和声音ListItem类:设置名称和位置init类:设置样式和界面(2)功能类actionPerformed类:添加音乐文件或文件夹play类:播放暂停设置controllerUpdate类:选择播放模式(二)系统UML类图MUSICPLAYERListIteminitprivate stati

5、c final long serialVersionUID = 1L;private String name;private String path;public ListItem()publicListItem(String name, String path) = name;this.path = path;import java.util.*;import javax.swing.JSlider;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.Point;frame = ne

6、w JFrame();frame.setTitleframe.setSizeframe.setResizableframe.setLocationRelativeToif (e instanceof EndOfMediaEvent)if (mode = 0)System.out.println("顺序播放");next(); else if (mode = 1)System.out.println("播放结束");player.setMediaTime(new Time(0);System.out.println("单曲循环");pl

7、ayer.start();else if (mode = 2)System.out.println("随机播放");rand();return;controllerUpdatebtnPlay.getIcon()=iconPlayplayer.start();System.out.println("暂停结束");isPause = false;player=Manager.createPlayer(new MediaLocator("file:"+currentItem.getPath();player.addControllerLis

8、tener(this)player.prefetch();playJFileChooser jfc = new JFileChooser();FileNameExtensionFilter filter=newFileNameExtensionFilter("音乐文件", "mp3", "wav");jfc.setFileFilter(filter);jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);jfc.setMultiSelectionEnabledactionPerformed

9、三.详细设计(一)核心技术介绍在Java.Applet包中的Applet类和AudioClip接口提供了播放声音的基本支持。以前计算机存储声音文件的格式有许多,目前Java只支持一种声音文件格式有.au,.mid,.wav等。对于其他格式的文件,可以用相应地工具软件将它们转换为.au或.mid等Java支持的文件。在Applet中播放声音文件有两种方法:(1)在Java的API中,Applet类提供了一个方法play(),它可以直接根据URL地址来播放声音。这个方法指定如下: Public void play(URL url); Public void play(URL url,string

10、soundFile);在第一种方法的定义中,参数url是保存声音文件的绝对URL地址,即统一资源定位,它指定Web网上的一个声音文件;在第二种方法定义中,参数URL是保存声音文件的基地址,而参数soundFile是声音文件的文件名。如果声音文件与Applet的HTML文件在相同的目录下,则可以通过使用getCodeBase()方法来获得该声音文件的基地址。如果Applet本身的URL地址为 “http/www.T Play(gatCodeBase(),“space.au”);等同于语句: Play(“http/在程序编写过程中,若使用第一种方法定义,则将程序由一个服务器转移到另一服务器上或从一

11、个目录转移到另一个目录下时,必须改变源代码中的URL地址:若使用第二种方法定义,即使用getCodeBase()方法来定位资源文件,则将程序由一个服务器转移到另一个服务器或从一个目录转移到另一个目录下时,不必改变源代码中的URL地址,而只需将声音文件与播放他的Java文件放在同一目录下即可。 Play()方法只能进行简单的播放,只能一次敬爱那个声音文件播放完,没有停止和连续播放的功能,所以,一般编写播放声音的程序时不采用此方法。(2)利用Java中的AudioClip类对象播放声音文件。AudioClip类提供了两种获取声音数据的方法,定义如下:AudioClip getAudioClip(U

12、RL url);AudioClip getAudioClip(URL url,String soundFile);它们的参数含义与以上相同。使用时可按如下步骤。)声明一个AudioClip类的对象,如: AudioClip audio;)使用getAudioClip()方法为AudioClip类的对象获得句柄,如:aud=getAudioClip(getCodeBase(),“space.au”);或一次完成,如: AudioClip aud=getAudioClip(getCodeBase(),“space.au”);或:AudioClip aud=getAudioClip(“HTTP/开始

13、使用这种方法创建声音播放程序文件的一般步骤如下: 装入 声音文件到AudioClip对象一次播放或者不停地循环播放声音停止播放结束Java.applet.AudioClip类提供了3个方法实现对声音文件播放的控制,它们分别是play(),loop()和stop()。在这个设计过程中使用了java.applet.AudioClip类中的play()方法播放一个载入的声音文件;使用loop()方法循环地播放该声音文件,并且使用stop()方法来停止播放这个声音文件。方法star()和stop()是Applet类中定义的方法,该程序继承了Applet类,并重新定义了这两个方法,这一过程称做方法的覆盖

14、。这两个方法是由操作系统自动调用的。当程序启动时两个方法,star()方法由操作系统自动调用,star()方法在运行时通过AudioClip对象aud调用loop()方法,实现对声音的循环播放。当程序停止运行时,Applet类中的stop()方法被自动调用,stop()方法运行时通过AudioClip对象aud调用AudioClip中的stop()方法来终止声音文件的播放。(二)创建播放器的图形用户界面所需的控件1菜单栏与菜单在程序中创建菜单是最常用图形的用户界面之一。Java语言中,与菜单相关类有5个:JMenuBar,JMenuItem,JCheckBoxMenuItem和JRadioBu

15、ttonMenuItem。 实现菜单的过程比较复杂,主要下面三步:(1)创建菜单栏,并将它设计到某个容器中。创建菜单栏需要利用JMenuBar类,该类只有一个默认构造方法(2)创建菜单条,并将它们添加到菜单栏中。创建菜单条需要利用JMenu类,该类有多种构造方法,其中最常用的是JMenu(String s)。(3)创建菜单项,并将它们添加到菜单条中。创建菜单项一般需要利用JMenuItem类,该类有多种构造方法,其中最常用的是JMenuItem(String text)。 2按钮与标签1)按钮创建按钮需要利用JButton类,JButton类中常用的方法有:public JButton() /

16、创建一个没有标记的空按钮public JButton(Icon icon) /创建一个标有指定图标的按钮public JButton(String text) /创建一个标有指定文字的按钮public JButton(String text ,Icon icon) /创建一个标有指定文字和图表的按钮2)标签标签与按钮相同,标签也可以显示一串文字或图像。创建标签需要利用JLabel类。JLabel类中常用的构造方法有:public JLabel()/创建一个空标签public JLabel(Icon image)/创建具有指定图标的标签public JLabel(Icon image,int h

17、orizontalAlignment)/创建具有指定图标和水平对齐方式的标签public JLabel(String text)/创建具有指定文字的标签public JLabel(String text,int horizontalAlignment)/创建具有指定文字和水平对齐方式的标签public JLabel(String text,Icon icon,int horizontalAlignment)/创建具有指定文字,图标和水平对齐方式的标签3列表框列表也允许用户从中进行选择。创建列表需要利用JList类,JList类中常用的构造方法有:public JList()/创建一个列表,列表

18、中的选项为空public JList(Object listData)/创建一个列表,列表中的选项由参数listData指定四 .系统实现主类MUSICPLAYER类:import java.util.*;import javax.swing.JSlider;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event

19、.MouseAdapter;import java.awt.event.MouseEvent;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;imp

20、ort java.io.Serializable;import javax.media.ControllerEvent;import javax.media.ControllerListener;import javax.media.EndOfMediaEvent;import javax.media.Manager;import javax.media.MediaLocator;import javax.media.NoPlayerException;import javax.media.Player;import javax.media.PrefetchCompleteEvent;impo

21、rt javax.media.Time;import javax.swing.ButtonGroup;import javax.swing.DefaultListModel;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JList;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JM

22、enuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JRadioButtonMenuItem;import javax.swing.JScrollBar;import javax.swing.JScrollPane;import javax.swing.filechooser.FileNameExtensionFilter;public class MusicPlayer implements ActionListener, Serializable,ControllerList

23、enerprivate static final long serialVersionUID = 1L;private JFrame frame = null;private JPanel controlPanel = null;private JButton btnPlay = null;private JButton btnPre = null;private JButton btnNext = null;private JScrollPane listPane = null;private JList list = null;private DefaultListModel listMo

24、del = null;private JMenuBar menubar = null;private JMenu menuFile = null, menuAbout = null, menuMode = null;private JMenuItem itemOpen, itemOpens, itemExit, itemAbout;private JRadioButtonMenuItem itemSingle, itemSequence ,itemRandom;private ListItem currentItem = null;private static Player player =

25、null;private boolean isPause = false;private int mode;private int currentIndex;private ImageIcon iconPlay = new ImageIcon("resimageplay.png");private ImageIcon iconPre = new ImageIcon("resimagepre.png");private ImageIcon iconNext = new ImageIcon("resimagenext.png");priv

26、ate ImageIcon iconPause = new ImageIcon("resimagepause.png");public static void main(String args)new MusicPlayer();public MusicPlayer()init();public void init()frame = new JFrame();frame.setTitle("音乐播放器");frame.setSize(500, 300);frame.setResizable(false);frame.setLocationRelative

27、To(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);menubar = new JMenuBar();menuFile = new JMenu("文件");menuAbout = new JMenu("关于");menuMode = new JMenu("播放模式");itemOpen = new JMenuItem("添加文件");itemOpens = new JMenuItem("添加文件夹");itemExit =

28、new JMenuItem("退出");itemAbout = new JMenuItem("关于");itemOpen.addActionListener(this);itemOpens.addActionListener(this);itemExit.addActionListener(this);itemAbout.addActionListener(this);itemSequence = new JRadioButtonMenuItem("顺序播放");itemSequence.setSelected(true);itemS

29、ingle = new JRadioButtonMenuItem("单曲循环");itemSequence.addActionListener(this);itemRandom = new JRadioButtonMenuItem("随机播放");itemRandom.addActionListener(this);itemSingle.addActionListener(this);ButtonGroup bg = new ButtonGroup();bg.add(itemRandom);bg.add(itemSequence);bg.add(item

30、Single);menuFile.add(itemOpen);menuFile.add(itemOpens);menuFile.add(itemExit);menuAbout.add(itemAbout);menuMode.add(itemSequence);menuMode.add(itemSingle);menuMode.add(itemRandom);menubar.add(menuFile);menubar.add(menuAbout);menubar.add(menuMode);frame.setJMenuBar(menubar);frame.setLayout(new Border

31、Layout();controlPanel = new JPanel();controlPanel.setLayout(new FlowLayout();btnPlay = new JButton(iconPlay);btnPre = new JButton(iconPre);btnNext = new JButton(iconNext);btnPlay.addActionListener(this);btnPre.addActionListener(this);btnNext.addActionListener(this);controlPanel.add(btnPre);controlPa

32、nel.add(btnPlay);controlPanel.add(btnNext);listPane = new JScrollPane();listModel = load();list = new JList(listModel);/获取音乐文件if (list.getSelectedIndex() = -1 && listModel.size() > 0)currentItem = (ListItem) listModel.get(0);list.setSelectedIndex(0);currentIndex=0;listPane.getViewport().a

33、dd(list);/添加点击响应list.addMouseListener(new MouseAdapter()public void mouseClicked(MouseEvent e)if (e.getClickCount() = 2)if(player!=null)player.close();btnPlay.setIcon(iconPlay);currentIndex = list.locationToIndex(e.getPoint();currentItem = (ListItem) listModel.get(currentIndex);list.setSelectedIndex

34、(currentIndex);play(););frame.setLayout(new BorderLayout();frame.add(controlPanel, BorderLayout.NORTH);frame.add(listPane, BorderLayout.CENTER);frame.setVisible(true);public void actionPerformed(ActionEvent e)if (e.getSource() = itemOpen)/ add filesJFileChooser jfc = new JFileChooser();FileNameExten

35、sionFilter filter = new FileNameExtensionFilter("音乐文件", "mp3", "wav");jfc.setFileFilter(filter);jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);jfc.setMultiSelectionEnabled(true);if (jfc.showOpenDialog(frame) = JFileChooser.APPROVE_OPTION)File files = jfc.getSelectedF

36、iles();for (File f : files)ListItem item = new ListItem(f.getName(), f.getAbsolutePath();listModel.addElement(item); else if (e.getSource() = itemOpens)/ add files in a directoryJFileChooser jfc = new JFileChooser();jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);if (jfc.showOpenDialog(frame

37、) = JFileChooser.APPROVE_OPTION)File directory = jfc.getSelectedFile();File files = directory.listFiles(new java.io.FileFilter()public boolean accept(File f)if (f.getName().toLowerCase().endsWith(".mp3")| f.getName().toLowerCase().endsWith(".wav")return true;return false;);for (F

38、ile file : files)ListItem item = new ListItem(file.getName(), file.getAbsolutePath();listModel.addElement(item);save(listModel); else if (e.getSource() = itemExit)System.exit(0); else if (e.getSource() = itemAbout)JOptionPane.showMessageDialog(frame, "作者:617"); else if (e.getSource() = btn

39、Play)/ play or pauseplay(); else if (e.getSource() = btnPre)pre(); else if (e.getSource() = btnNext)/ next musicnext(); else if (e.getSource() = itemSequence)mode = 0; else if (e.getSource() = itemSingle)mode = 1;else if (e.getSource() = itemRandom)mode = 2;/ play control/* * * 播放*/public void play(

40、)if (btnPlay.getIcon() = iconPlay)if (isPause)player.start();System.out.println("播放");isPause = false; elsetryplayer = Manager.createPlayer(new MediaLocator("file:"+ currentItem.getPath();player.addControllerListener(this); / 提取媒体内容player.prefetch(); catch (NoPlayerException e1)e

41、1.printStackTrace(); catch (IOException e1)e1.printStackTrace();btnPlay.setIcon(iconPause); elseplayer.stop();isPause = true;System.out.println("暂停");btnPlay.setIcon(iconPlay); /播放暂停图标转换/* * * 下一曲*/public void next()if (currentIndex = listModel.getSize() - 1)currentIndex = 0; elsecurrentIn

42、dex+;currentItem = (ListItem) listModel.get(currentIndex);list.setSelectedIndex(currentIndex);Point p = list.indexToLocation(currentIndex);JScrollBar jScrollBar = listPane.getVerticalScrollBar();/ 获得垂直转动条jScrollBar.setValue(p.y);/ 设置垂直转动条位置btnPlay.setIcon(iconPlay);if (player != null)player.close();

43、isPause = false;play();public void rand()list.setSelectedIndex(int)(Math.random()%(listModel.getSize()-1);Point p=list.indexToLocation(int)(Math.random()%(listModel.getSize()-1);JScrollBar jScrollBar = listPane.getVerticalScrollBar();jScrollBar.setValue(p.y);btnPlay.setIcon(iconPlay);if (player != n

44、ull)player.close();isPause = false;play();/* * * 上一曲*/public void pre()if (currentIndex = 0)currentIndex = listModel.getSize() - 1; elsecurrentIndex-;currentItem = (ListItem) listModel.get(currentIndex);list.setSelectedIndex(currentIndex);Point p = list.indexToLocation(currentIndex);JScrollBar jScro

45、llBar = listPane.getVerticalScrollBar();/ 获得垂直转动条jScrollBar.setValue(p.y);/ 设置垂直转动条位置btnPlay.setIcon(iconPlay);if (player != null)player.close();isPause = false;play();/ end play controlpublic DefaultListModel load()File file = new File("list.lst");DefaultListModel dlm = new DefaultListMod

46、el();if (file.exists()tryObjectInputStream ois = new ObjectInputStream(new FileInputStream(file);Integer size = (Integer) ois.readObject();if (size != 0)for (int i = 0; i < size; i+)ListItem item = (ListItem) ois.readObject();dlm.addElement(item);ois.close();return dlm; catch (FileNotFoundExcepti

47、on e)e.printStackTrace(); catch (IOException e)e.printStackTrace(); catch (ClassNotFoundException e)e.printStackTrace();return dlm;public void save(DefaultListModel dlm)tryObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("list.lst");Integer size = dlm.getSize();oos.writeObject(size);for (int i = 0; i < size; i+)ListItem item = (ListItem) dlm.get(i);oos.writeObject(item);oos.close(); catch (Exception e)e.printStackTrace();public void

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论