利用java制作简单的音乐播放器
本文主要是用到java中的swing技术,以及JMFjar中的API,为大家分享了java音乐播放器的具体实现代码,供大家参考,具体内容如下
备注:需要用JDK1.8才能播放音乐MP3
packagebaidu; importjava.awt.*; importjava.awt.event.*; importjava.io.*; importjava.util.*; importjavax.swing.*; importjavax.media.bean.playerbean.*;//这个包要用到JMF publicclassMP3extendsJFrameimplementsRunnable{ publicJLabelshijian; privateJSlidersldDiameter; publicMediaPlayersoumd1; publicJButtonplaySound,loopSound,stopsound; publicJButtontjian,shanc,baocun,duqu; publicJPaneljp1,jp2,jp3,jp4; publicJListjl; intzongmiao=0; publicVectorvector,mingcheng; booleanfo=false,geshi=false; JLabeljl1,jl2,sj1,sj2; JTextFieldjt1,jt2; JButtonqueding,xiugai; intzong=0; inta=0,b=0,you=1,mm=0; intfenzhong,miaozhong; publicMP3(){ super("java简单音乐播放器--严楷"); soumd1=newMediaPlayer(); Containerc=getContentPane(); c.setLayout(newFlowLayout()); mingcheng=newVector(); jp1=newJPanel(); shijian=newJLabel(); jp1.add(shijian); c.add(jp1); playSound=newJButton("开始播放"); loopSound=newJButton("循环播放"); stopsound=newJButton("停止播放"); jp2=newJPanel(); jp2.add(playSound); jp2.add(loopSound); jp2.add(stopsound); c.add(jp2); jp4=newJPanel(); sj1=newJLabel(); sj2=newJLabel(); sldDiameter=newJSlider(SwingConstants.HORIZONTAL,0,100,0); sldDiameter.setMajorTickSpacing(1); sldDiameter.setPaintTicks(true); jp4.add(sj1); jp4.add(sldDiameter); jp4.add(sj2); c.add(jp4); vector=newVector(); jl=newJList(mingcheng); jl.setVisibleRowCount(5); jl.setFixedCellHeight(40); jl.setFixedCellWidth(265); jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); c.add(newJScrollPane(jl)); tjian=newJButton("添加"); shanc=newJButton("删除"); duqu=newJButton("读取"); baocun=newJButton("保存"); jp3=newJPanel(); jp3.add(tjian); jp3.add(shanc); jp3.add(baocun); jp3.add(duqu); c.add(jp3); try{ Strings="C:/Users/Administrator/Music/卓依婷-萍聚.mp3"; ObjectInputStreaminput=newObjectInputStream( newFileInputStream(s)); lujina1=(lujin)input.readObject(); mingcheng=a1.b; vector=a1.a; jl.setListData(mingcheng); }catch(Exceptione){ System.out.println("c盘没有保存音乐文件"); //e.printStackTrace(); } baocun.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEventevent){ JFileChooserfileChooser=newJFileChooser();//实例化文件选择器 fileChooser .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置文件选择模式,此处为文件和目录均可 if(fileChooser.showSaveDialog(MP3.this)==JFileChooser.APPROVE_OPTION){//弹出文件选择器,并判断是否点击了打开按钮 StringfileName=fileChooser.getSelectedFile() .getAbsolutePath();//得到选择文件或目录的绝对路径 mmm(vector,mingcheng,fileName); } } }); duqu.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEventevent){ JFileChooserfileChooser=newJFileChooser();//实例化文件选择器 fileChooser .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置文件选择模式,此处为文件和目录均可 if(fileChooser.showOpenDialog(MP3.this)==JFileChooser.APPROVE_OPTION){//弹出文件选择器,并判断是否点击了打开按钮 StringfileName=fileChooser.getSelectedFile() .getAbsolutePath();//得到选择文件或目录的绝对路径 try{ ObjectInputStreaminput=newObjectInputStream( newFileInputStream(fileName)); lujina1=(lujin)input.readObject(); mingcheng=a1.b; vector=a1.a; jl.setListData(mingcheng); }catch(Exceptione){ e.printStackTrace(); } } } }); playSound.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ if(jl.getSelectedIndex()>=0){ Stringyy=(String)vector.get(jl.getSelectedIndex()); Fileff=newFile(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(InterruptedExceptioneee){ } zongmiao=(int)soumd1.getDuration().getSeconds(); if(zongmiao>10000) { try{ Thread.sleep(500); }catch(InterruptedExceptionew){ } zongmiao=(int)soumd1.getDuration().getSeconds(); } zongmiao=(int)soumd1.getDuration().getSeconds(); Stringaa=fen(zongmiao); sj2.setText(aa); }else JOptionPane.showMessageDialog(null, "播放文件格式的有错,无法播放建议删除"); }else JOptionPane.showMessageDialog(null, "此歌曲文件已经不存在,建议删除"); } else JOptionPane.showMessageDialog(null,"请选择音乐文件"); } }); loopSound.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ if(jl.getSelectedIndex()>=0){ Stringyy=(String)vector.get(jl.getSelectedIndex()); Fileff=newFile(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(InterruptedExceptionwe){ } zongmiao=(int)soumd1.getDuration().getSeconds(); if(zongmiao>10000) { try{ Thread.sleep(500); }catch(InterruptedExceptionwe){ } zongmiao=(int)soumd1.getDuration().getSeconds(); } zongmiao=(int)soumd1.getDuration().getSeconds(); Stringaa=fen(zongmiao); sj2.setText(aa); }else JOptionPane.showMessageDialog(null, "播放文件格式的有错,无法播放建议删除"); }else JOptionPane.showMessageDialog(null, "此歌曲文件已经不存在,建议删除"); } else JOptionPane.showMessageDialog(null,"请选择音乐文件"); } }); stopsound.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ a=0; b=0; you=0; mm=0; geshi=false; fo=false; sldDiameter.setMaximum(100); sldDiameter.setValue(0); sj1.setText(null); sj2.setText(null); if(jl.getSelectedIndex()>=0) soumd1.stop(); } }); tjian.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEventevent){ JFileChooserfileChooser=newJFileChooser();//实例化文件选择器 fileChooser .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置文件选择模式,此处为文件和目录均可 fileChooser.setCurrentDirectory(newFile("."));//设置文件选择器当前目录 fileChooser .setFileFilter(newjavax.swing.filechooser.FileFilter(){ publicbooleanaccept(Filefile){//可接受的文件类型 Stringname=file.getName().toLowerCase(); returnname.endsWith(".mp3") ||file.isDirectory(); } publicStringgetDescription(){//文件描述 return"音乐文件(*.mp3)"; } }); if(fileChooser.showOpenDialog(MP3.this)==JFileChooser.APPROVE_OPTION){//弹出文件选择器,并判断是否点击了打开按钮 StringfileName=fileChooser.getSelectedFile() .getAbsolutePath();//得到选择文件或目录的绝对路径 vector.add(fileName); StringBufferbuffer=daoxu(fileName); mingcheng.add(buffer); } } }); shanc.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEventevent){ if(jl.getSelectedIndex()>=0){ mingcheng.removeElementAt(jl.getSelectedIndex()); vector.removeElementAt(jl.getSelectedIndex()); jl.setListData(mingcheng); } } }); jl.addMouseListener(newMouseAdapter(){ publicvoidmousePressed(MouseEventevent){ if(event.getClickCount()==2){ if(jl.getSelectedIndex()>=0){ Stringyy=(String)vector.get(jl.getSelectedIndex()); Fileff=newFile(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(InterruptedExceptione){ } zongmiao=(int)soumd1.getDuration().getSeconds(); if(zongmiao>10000) { try{ Thread.sleep(500); }catch(InterruptedExceptione){ } zongmiao=(int)soumd1.getDuration().getSeconds(); } Stringaa=fen(zongmiao); sj2.setText(aa); }else JOptionPane.showMessageDialog(null, "播放文件格式的有错,无法播放建议删除"); }else JOptionPane.showMessageDialog(null, "此歌曲文件已经不存在,建议删除"); } } if(event.isMetaDown()){ if(jl.getSelectedIndex()>=0){ inta=jl.getSelectedIndex(); xiugaix=newxiugai(); jt1.setText(mingcheng.get(jl.getSelectedIndex())+""); jt2.setText(vector.get(jl.getSelectedIndex())+""); } } } }); addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ Strings="c:\\music.txt"; mmm(vector,mingcheng,s); System.exit(0); } }); setSize(300,400); setVisible(true); } publicvoidrun(){ while(true){ Datenow=newDate(); //shijian.setText("当前时间:"+now.getYear()+"年"+now.getMonth()+"月"+now.getDate()+"日"+now.getTime()); shijian.setText("当前时间:"+(newjava.text.SimpleDateFormat("yyyy-MM-ddHH:mm:ss")).format(newDate())+"严楷"); try{ Thread.sleep(1000); }catch(InterruptedExceptione){ } if(geshi){ you=(int)soumd1.getMediaTime().getSeconds(); mm=you%60; if(you!=0) if(you%60==0) { b++; } sj1.setText(b+":"+mm); sldDiameter.setMaximum(zongmiao); if(you!=zongmiao) sldDiameter.setValue(you); else{ sldDiameter.setValue(0); mm=0; b=0; } } } } publicStringBufferdaoxu(StringfileName){ Stringmc=""; for(inti=fileName.length();i>=1;i--){ if(fileName.charAt(i-1)=='\\') break; mc+=fileName.charAt(i-1); } StringBufferbuffer=newStringBuffer(mc); StringBuffermm=buffer.reverse(); returnmm; } publicvoidmmm(Vectorvector,Vectormingcheng,Stringlujin){ lujina=newlujin(vector,mingcheng); try{ ObjectOutputStreamoutput=newObjectOutputStream(newFileOutputStream(lujin)); output.writeObject(a); output.flush(); output.close(); }catch(Exceptione){ } } publicStringfen(intyy){ fenzhong=(int)yy/60; miaozhong=(int)yy%60; Stringsss=fenzhong+":"+miaozhong; returnsss; } publicstaticvoidmain(Stringagrs[]){ MP3s=newMP3(); Threadt1=newThread(s); t1.start(); s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } classxiugaiextendsJFrame{ publicxiugai(){ jl1=newJLabel("文件名"); jt1=newJTextField(20); jl2=newJLabel("文件路径"); jt2=newJTextField(20); xiugai=newJButton("修改"); Containerc=getContentPane(); c.setLayout(newGridLayout(3,1)); JPanelj1=newJPanel(); JPanelj2=newJPanel(); JPanelj3=newJPanel(); j1.add(jl1); j1.add(jt1); j2.add(jl2); j2.add(jt2); j3.add(xiugai); c.add(j1); c.add(j2); c.add(j3); xiugai.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEventevent){ mingcheng.setElementAt(jt1.getText(),jl.getSelectedIndex()); vector.setElementAt(jt2.getText(),jl.getSelectedIndex()); jl.setListData(mingcheng); dispose(); } }); setSize(300,120); setVisible(true); } } } classlujinimplementsSerializable{ Vectora=newVector(); Vectorb=newVector(); publiclujin(Vectorvector,Vectormingcheng){ a=vector; b=mingcheng; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。