音樂播放器Java課程設計報告_第1頁
音樂播放器Java課程設計報告_第2頁
音樂播放器Java課程設計報告_第3頁
音樂播放器Java課程設計報告_第4頁
音樂播放器Java課程設計報告_第5頁
免費預覽已結束,剩余11頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、M林電牙科被大喙GUILIN UNIVERSITY OF ELECTRONIC TECHNOLOGYJAVA程序設計課程設計報告題目:音樂播放器學院:計算機科學與工程學院專業(yè):信息管理與信息系統(tǒng)學生姓名:楊柏昌學號:1400320228指導老師:汪華登1. 概要設計 2. 詳細設計 3. 測試 4. 總結 概要設計播放器主要使用JMF框架實現(xiàn),播放器界面如下正在懵曲r未遺取強曲O咂席睛曲 O單曲循環(huán) 隨機睡湎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( " 單曲循環(huán) " );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() / 對歌曲點擊的監(jiān)聽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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論