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

上传人:痛*** 文档编号:77964443 上传时间:2022-04-20 格式:DOC 页数:16 大小:457.50KB
返回 下载 相关 举报
音乐播放器Java课程设计报告_第1页
第1页 / 共16页
音乐播放器Java课程设计报告_第2页
第2页 / 共16页
音乐播放器Java课程设计报告_第3页
第3页 / 共16页
点击查看更多>>
资源描述
JAVA程序设计课程设计报告 题目:音乐播放器 学院:计算机科学与工程学院 专业:信息管理与信息系统 学生姓名:杨柏昌 学号: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,ControllerListenerPlayer 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(new MediaLocator(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); else return; if(e.getSource()=beforeBtn) int n=list.getItemCount(); if(songIndexn) songIndex=songIndex-1; if(songIndex0) songIndex=n-1; fileName=list.getItem(songIndex); run(); if(e.getSource()=nextBtn) int n=list.getItemCount(); 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(new MediaLocator(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(songIndexn) fileName=list.getItem(songIndex+1); songIndex=songIndex+1; run(); if(loop=2) int n=list.getItemCount(); if(songIndexn) 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。从书上和网上学了些JMF的知识,做个播放器难度不大。在做到对于播放模式的实现时卡了一下,最终参考了其他播放器的做法,用了loop这个标记来实现播放模式的转换。
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!