音乐播放器Java课程设计报告_第1页
音乐播放器Java课程设计报告_第2页
音乐播放器Java课程设计报告_第3页
音乐播放器Java课程设计报告_第4页
音乐播放器Java课程设计报告_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

1、M林电牙科被大喙GUILIN UNIVERSITY OF ELECTRONIC TECHNOLOGYJAVA程序设计课程设计报告题目:音乐播放器学院:计算机科学与工程学院专业:信息管理与信息系统学生姓名:杨柏昌学号:1400320228指导老师:汪华登1. 概要设计 2. 详细设计 3. 测试 4. 总结 概要设计播放器主要使用JMF框架实现,播放器界面如下正在懵曲r未遗取强曲O咂席睛曲 O单曲循环 随机睡湎I上一首I.就I暂停I停止I 一首款曲列*选择文件夹 退出详细设计package design.curriculum.JAVA;import java.awt.*;import java.

2、awt.event.*;import javax.swing.*;import javax.swing.border.Border;import java.io.*;import javax.media.*;public class JMFPlayer extends JFrame implementsActionListener,ControllerListenerPlayer player =null ; / 声明一个 Player 对象StringfilePath; /存放音乐文件夹所在目录StringfileName ; /存放歌曲名称boolean click =false ; /用

3、来标记歌曲是否被点击int songIndex ; /歌曲元素在列表中的位置int loop =3; /作为播放模式的标记JLabel songNameLable ;JRadioButtonsequenceBtn;JRadioButtonsingleBtn ;JRadioButtonrandomBtn ;ButtonGroup loopGroup ;JButton beforeBtn ;JButtonnextBtn;JButtonplayBtn;JButton pauseBtn ;JButtonstopBtn;JButtonfileBtn;JButtonexitBtn;Component co

4、ntrolComponent ; /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(); / 获得容器主面板c

5、ontainer .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.createEt

6、chedBorder ();loopjp =new JPanel();loopjp .setSize(400,50);container .add( loopjp );sequenceBtn =new JRadioButton( " 顺序播放 " );sequenceBtn .addActionListener(new ActionListener() public void actionPerformed(ActionEventevt )loop = 1;);singleBtn =new JRadioButton( " 单曲循环 " );singleB

7、tn .addActionListener(new ActionListener() public void actionPerformed(ActionEventevt )loop = 0; );randomBtn =new JRadioButton( " 随机播放 " );randomBtn .addActionListener(new ActionListener() public void actionPerformed(ActionEventevt )loop = 2;);loopGroup =new ButtonGroup();loopGroup .add( s

8、equenceBtn );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( &qu

9、ot; 上一首 " );playBtn =new JButton(" 播放 " );pauseBtn =new JButton( " 暂停 " );stopBtn =new JButton(" 停止 " );nextBtn =new JButton( " 下一首 " );beforeBtn .addActionListener( this );playBtn.addActionListener(this);pauseBtn .addActionListener( this );stopBtn.addAct

10、ionListener(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. createEtchedBorde

11、r ();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,

12、50);container .add( btn2jp );btn2jp .add( fileBtn);btn2jp .add( exitBtn);list =new List();list.setBackground(Color.pink );JScrollPanesongListScroll = new JScrollPane( list );listjp .add( songListScroll, BorderLayout.CENTER);list .addMouseListener( new MouseAdapter() / 对歌曲点击的监听public void mouseClicke

13、d(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();

14、/* 对按钮事件的处理*/public void actionPerformed(ActionEvente)if ( e.getSource()= fileBtn )FileDialogdialog=new FileDialog(this, " 音乐 " ,FileDialogdialog .setVisible(true );filePath = dialog.getDirectory();if ( filePath != null ) LOAD);File filedir = new File( filePath );File filelist = filedir .l

15、istFiles(); 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 ;tryif ( player != null ) player .stop();player .

16、deallocate();if ( controlComponent != null )songNamejp .remove( controlComponent ); songNamejp .validate();player =Manager. createPlayer( newMediaLocator("file:"+filePath +fileName);player.getDuration();player.prefetch();player.addControllerListener(thissongNameLable .setText( fileName );c

17、atch (IOExceptione3) catch (NoPlayerExceptione2) 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);el

18、se return ;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 (

19、 songIndex >=n) songIndex =0;fileName =list .getItem( songIndex );run();public void run() tryif ( player != null ) player .stop(); player .deallocate();if ( controlComponent != null ) songNamejp .remove( controlComponent ); songNamejp .validate();player =Manager. createPlayer ( newMediaLocator( &

20、quot;file:" +filePath +fileName );player .getDuration();player .prefetch();player .addControllerListener(this );songNameLable .setText( fileName );catch (IOException e3 ) catch (NoPlayerException e2) /* * 对控制面板事件的处理* */ public synchronized void controllerUpdate(ControllerEventevent )if ( event

21、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)songInde

温馨提示

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

评论

0/150

提交评论