利用java制作简单的音乐播放器
程序员文章站
2023-12-18 21:36:28
本文主要是用到java中的swing技术,以及jmfjar中的api,为大家分享了java音乐播放器的具体实现代码,供大家参考,具体内容如下
备注:需要用jdk1.8才能...
本文主要是用到java中的swing技术,以及jmfjar中的api,为大家分享了java音乐播放器的具体实现代码,供大家参考,具体内容如下
备注:需要用jdk1.8才能播放音乐mp3
package baidu; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.media.bean.playerbean.*; //这个包要用到jmf public class mp3 extends jframe implements runnable { public jlabel shijian; private jslider slddiameter; public mediaplayer soumd1; public jbutton playsound, loopsound, stopsound; public jbutton tjian, shanc, baocun, duqu; public jpanel jp1, jp2, jp3, jp4; public jlist jl; int zongmiao=0; public vector vector, mingcheng; boolean fo = false, geshi = false; jlabel jl1, jl2, sj1, sj2; jtextfield jt1, jt2; jbutton queding, xiugai; int zong = 0; int a = 0, b = 0, you = 1,mm=0; int fenzhong, miaozhong; public mp3() { super("java简单音乐播放器--严楷"); soumd1 = new mediaplayer(); container c = getcontentpane(); c.setlayout(new flowlayout()); mingcheng = new vector(); jp1 = new jpanel(); shijian = new jlabel(); jp1.add(shijian); c.add(jp1); playsound = new jbutton("开始播放"); loopsound = new jbutton("循环播放"); stopsound = new jbutton("停止播放"); jp2 = new jpanel(); jp2.add(playsound); jp2.add(loopsound); jp2.add(stopsound); c.add(jp2); jp4 = new jpanel(); sj1 = new jlabel(); sj2 = new jlabel(); slddiameter = new jslider(swingconstants.horizontal, 0, 100, 0); slddiameter.setmajortickspacing(1); slddiameter.setpaintticks(true); jp4.add(sj1); jp4.add(slddiameter); jp4.add(sj2); c.add(jp4); vector = new vector(); jl = new jlist(mingcheng); jl.setvisiblerowcount(5); jl.setfixedcellheight(40); jl.setfixedcellwidth(265); jl.setselectionmode(listselectionmodel.single_selection); c.add(new jscrollpane(jl)); tjian = new jbutton("添加"); shanc = new jbutton("删除"); duqu = new jbutton("读取"); baocun = new jbutton("保存"); jp3 = new jpanel(); jp3.add(tjian); jp3.add(shanc); jp3.add(baocun); jp3.add(duqu); c.add(jp3); try { string s =" c:/users/administrator/music/卓依婷 - 萍聚.mp3"; objectinputstream input = new objectinputstream( new fileinputstream(s)); lujin a1 = (lujin) input.readobject(); mingcheng = a1.b; vector = a1.a; jl.setlistdata(mingcheng); } catch (exception e) { system.out.println("c盘没有保存音乐文件"); //e.printstacktrace(); } baocun.addactionlistener(new actionlistener() { public void actionperformed(actionevent event) { jfilechooser filechooser = new jfilechooser(); // 实例化文件选择器 filechooser .setfileselectionmode(jfilechooser.files_and_directories); // 设置文件选择模式,此处为文件和目录均可 if (filechooser.showsavedialog(mp3.this) == jfilechooser.approve_option) { // 弹出文件选择器,并判断是否点击了打开按钮 string filename = filechooser.getselectedfile() .getabsolutepath(); // 得到选择文件或目录的绝对路径 mmm(vector, mingcheng, filename); } } }); duqu.addactionlistener(new actionlistener() { public void actionperformed(actionevent event) { jfilechooser filechooser = new jfilechooser(); // 实例化文件选择器 filechooser .setfileselectionmode(jfilechooser.files_and_directories); // 设置文件选择模式,此处为文件和目录均可 if (filechooser.showopendialog(mp3.this) == jfilechooser.approve_option) { // 弹出文件选择器,并判断是否点击了打开按钮 string filename = filechooser.getselectedfile() .getabsolutepath(); // 得到选择文件或目录的绝对路径 try { objectinputstream input = new objectinputstream( new fileinputstream(filename)); lujin a1 = (lujin) input.readobject(); mingcheng = a1.b; vector = a1.a; jl.setlistdata(mingcheng); } catch (exception e) { e.printstacktrace(); } } } }); playsound.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { if (jl.getselectedindex() >= 0) { string yy = (string) vector.get(jl.getselectedindex()); file ff = new file(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 (interruptedexception eee) { } zongmiao=(int)soumd1.getduration().getseconds(); if(zongmiao>10000) { try { thread.sleep(500); } catch (interruptedexception ew) { } zongmiao=(int)soumd1.getduration().getseconds(); } zongmiao=(int)soumd1.getduration().getseconds(); string aa=fen(zongmiao); sj2.settext(aa); } else joptionpane.showmessagedialog(null, "播放文件格式的有错,无法播放 建议删除"); } else joptionpane.showmessagedialog(null, "此歌曲文件已经不存在,建议删除"); } else joptionpane.showmessagedialog(null, "请选择音乐文件"); } }); loopsound.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { if (jl.getselectedindex() >= 0) { string yy = (string) vector.get(jl.getselectedindex()); file ff = new file(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 (interruptedexception we) { } zongmiao=(int)soumd1.getduration().getseconds(); if(zongmiao>10000) { try { thread.sleep(500); } catch (interruptedexception we) { } zongmiao=(int)soumd1.getduration().getseconds(); } zongmiao=(int)soumd1.getduration().getseconds(); string aa=fen(zongmiao); sj2.settext(aa); } else joptionpane.showmessagedialog(null, "播放文件格式的有错,无法播放 建议删除"); } else joptionpane.showmessagedialog(null, "此歌曲文件已经不存在,建议删除"); } else joptionpane.showmessagedialog(null, "请选择音乐文件"); } }); stopsound.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { 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(new actionlistener() { public void actionperformed(actionevent event) { jfilechooser filechooser = new jfilechooser(); // 实例化文件选择器 filechooser .setfileselectionmode(jfilechooser.files_and_directories); // 设置文件选择模式,此处为文件和目录均可 filechooser.setcurrentdirectory(new file(".")); // 设置文件选择器当前目录 filechooser .setfilefilter(new javax.swing.filechooser.filefilter() { public boolean accept(file file) { // 可接受的文件类型 string name = file.getname().tolowercase(); return name.endswith(".mp3") || file.isdirectory(); } public string getdescription() { // 文件描述 return "音乐文件(*.mp3)"; } }); if (filechooser.showopendialog(mp3.this) == jfilechooser.approve_option) { // 弹出文件选择器,并判断是否点击了打开按钮 string filename = filechooser.getselectedfile() .getabsolutepath(); // 得到选择文件或目录的绝对路径 vector.add(filename); stringbuffer buffer = daoxu(filename); mingcheng.add(buffer); } } }); shanc.addactionlistener(new actionlistener() { public void actionperformed(actionevent event) { if (jl.getselectedindex() >= 0) { mingcheng.removeelementat(jl.getselectedindex()); vector.removeelementat(jl.getselectedindex()); jl.setlistdata(mingcheng); } } }); jl.addmouselistener(new mouseadapter() { public void mousepressed(mouseevent event) { if (event.getclickcount() == 2) { if (jl.getselectedindex() >= 0) { string yy = (string) vector.get(jl.getselectedindex()); file ff = new file(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 (interruptedexception e) { } zongmiao=(int)soumd1.getduration().getseconds(); if(zongmiao>10000) { try { thread.sleep(500); } catch (interruptedexception e) { } zongmiao=(int)soumd1.getduration().getseconds(); } string aa=fen(zongmiao); sj2.settext(aa); } else joptionpane.showmessagedialog(null, "播放文件格式的有错,无法播放 建议删除"); } else joptionpane.showmessagedialog(null, "此歌曲文件已经不存在,建议删除"); } } if (event.ismetadown()) { if (jl.getselectedindex() >= 0) { int a = jl.getselectedindex(); xiugai x = new xiugai(); jt1.settext(mingcheng.get(jl.getselectedindex()) + ""); jt2.settext(vector.get(jl.getselectedindex()) + ""); } } } }); addwindowlistener(new windowadapter() { public void windowclosing(windowevent e) { string s = "c:\\music.txt"; mmm(vector, mingcheng, s); system.exit(0); } }); setsize(300, 400); setvisible(true); } public void run() { while (true) { date now = new date(); //shijian.settext("当前时间: " + now.getyear()+"年"+now.getmonth()+"月"+now.getdate()+"日"+now.gettime()); shijian.settext("当前时间:"+(new java.text.simpledateformat("yyyy-mm-dd hh:mm:ss")).format(new date())+" 严楷"); try { thread.sleep(1000); } catch (interruptedexception e) { } 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; } } } } public stringbuffer daoxu(string filename) { string mc = ""; for (int i = filename.length(); i >= 1; i--) { if (filename.charat(i - 1) == '\\') break; mc += filename.charat(i - 1); } stringbuffer buffer = new stringbuffer(mc); stringbuffer mm = buffer.reverse(); return mm; } public void mmm(vector vector, vector mingcheng, string lujin) { lujin a = new lujin(vector, mingcheng); try { objectoutputstream output = new objectoutputstream(new fileoutputstream(lujin)); output.writeobject(a); output.flush(); output.close(); } catch (exception e) { } } public string fen(int yy) { fenzhong = (int) yy / 60; miaozhong = (int) yy % 60; string sss = fenzhong + ":" + miaozhong; return sss; } public static void main(string agrs[]) { mp3 s = new mp3(); thread t1 = new thread(s); t1.start(); s.setdefaultcloseoperation(jframe.exit_on_close); } class xiugai extends jframe { public xiugai() { jl1 = new jlabel("文件名"); jt1 = new jtextfield(20); jl2 = new jlabel("文件路径"); jt2 = new jtextfield(20); xiugai = new jbutton("修改"); container c = getcontentpane(); c.setlayout(new gridlayout(3, 1)); jpanel j1 = new jpanel(); jpanel j2 = new jpanel(); jpanel j3 = new jpanel(); 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(new actionlistener() { public void actionperformed(actionevent event) { mingcheng.setelementat(jt1.gettext(), jl.getselectedindex()); vector.setelementat(jt2.gettext(), jl.getselectedindex()); jl.setlistdata(mingcheng); dispose(); } }); setsize(300, 120); setvisible(true); } } } class lujin implements serializable { vector a = new vector(); vector b = new vector(); public lujin(vector vector, vector mingcheng) { a = vector; b = mingcheng; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。