java 实现音乐播放器的简单实例
程序员文章站
2024-03-02 08:39:46
java 实现音乐播放器的简单实例
实现效果图:
代码如下
package cn.hncu.games;
import java.applet.ap...
java 实现音乐播放器的简单实例
实现效果图:
代码如下
package cn.hncu.games; import java.applet.applet; import java.applet.audioclip; import java.awt.color; import java.awt.font; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.io.file; import java.net.url; import javax.swing.defaultlistmodel; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jlist; import javax.swing.listmodel; import javax.swing.event.listselectionevent; import javax.swing.event.listselectionlistener; public class musicplayer extends jframe{ //显示(歌曲名称)播放状态的标签 jlabel songnamelabel = null; //四个播放功能键按钮 jbutton btnlast = null; //上一曲 jbutton btnplay = null; //播放/停止 jbutton btnnext = null; //下一曲 jbutton btnloop = null; //循环 //歌曲列表 jlist songslist = null; audioclip songs[] = null; audioclip currentsong = null; int index=0; //当前歌曲在jlist中的位置(序号) //歌曲文件名数组---string string[] strsongnames={ "song1.wav","song2.wav","song3.wav","song4.wav","song5.wav","song6.wav" }; final string dir="songs\\"; //播放音乐的线程 thread playerthread=null; boolean isplayorstop = true;//true代表播放状态 boolean isloop = false; //是否为循环状态 public musicplayer() { super("音乐播放器"); setbounds(300, 50, 310, 500); setdefaultcloseoperation(exit_on_close); setlayout(null); //hello(); //显示(歌曲名称)播放状态的标签 songnamelabel = new jlabel(); font songnamefont = new font("黑体",font.italic,18); songnamelabel.setfont(songnamefont); songnamelabel.settext("我的音乐播放器"); songnamelabel.setbounds(10, 10, 300, 40); getcontentpane().add(songnamelabel); //四个播放功能键按钮 btnlast = new jbutton(); btnplay = new jbutton(); btnnext = new jbutton(); btnloop = new jbutton(); //位置大小 btnlast.setbounds(10, 70, 50, 40); btnplay.setbounds(70, 70, 50, 40); btnnext.setbounds(130, 70, 50, 40); btnloop.setbounds(190, 70, 50, 40); //设置图片 btnlast.seticon( new imageicon("images2/1.png")); btnplay.seticon( new imageicon("images2/2.png")); btnnext.seticon( new imageicon("images2/3.png")); btnloop.seticon( new imageicon("images2/4.png")); //添加到框架 getcontentpane().add(btnlast); getcontentpane().add(btnplay); getcontentpane().add(btnnext); getcontentpane().add(btnloop); //添加监听 mymouselistener mml = new mymouselistener(); btnlast.addmouselistener(mml); btnplay.addmouselistener(mml); btnnext.addmouselistener(mml); btnloop.addmouselistener(mml); //歌曲列表的标题 jlabel listlabel = new jlabel("播放列表"); listlabel.setbounds(10, 120, 100, 30); font listlabelfont = new font("黑体",font.bold,16); listlabel.setfont(listlabelfont); getcontentpane().add(listlabel); //歌曲列表 /* songslist = new jlist(); songslist.setbounds(10, 150, 250, 300); songslist.setbackground(color.cyan); //把所有歌曲名逐个添加到list中 //songslist.setlistdata(strsongnames); for(int i=0;i<strsongnames.length;i++){ defaultlistmodel dm = (defaultlistmodel)songslist.getmodel(); dm.add(i,strsongnames[i]); } getcontentpane().add(songslist); */ defaultlistmodel lm = new defaultlistmodel(); songslist = new jlist(lm); songslist.setbounds(10, 150, 250, 300); songslist.setbackground(color.cyan); //把所有歌曲名逐个添加到list中 //songslist.setlistdata(strsongnames); songs = new audioclip[strsongnames.length]; for(int i=0;i<strsongnames.length;i++){ lm.add(i,strsongnames[i]); songs[i] = loadsound(strsongnames[i]); } getcontentpane().add(songslist); //lm.remove(3); //对jlist控件的监听技术实现 songslist.addlistselectionlistener(new listselectionlistener() { @override public void valuechanged(listselectionevent e) { currentsong.stop(); index = songslist.getselectedindex(); isplayorstop = true; playerthread = new thread( new musicrun() ); playerthread.start(); } }); //单开一个线程,专用于播放音乐 playerthread = new thread( new musicrun() ); playerthread.start(); setvisible(true); } private audioclip loadsound(string filename) { try { url url = new url("file:songs\\"+filename); audioclip au = applet.newaudioclip(url); return au; } catch (exception e) { e.printstacktrace(); } return null; } //讲解音乐播放的基本技术 private void hello() { try { file f = new file("songs\\song1.wav"); url url = f.touri().tourl(); //url url = new url("file:songs\\song1.wav"); audioclip au = applet.newaudioclip(url); au.play(); //au.loop(); //au.stop(); } catch (exception e) { e.printstacktrace(); } } private class mymouselistener extends mouseadapter{ @override public void mouseclicked(mouseevent e) { jbutton btn = (jbutton) e.getsource(); currentsong.stop(); if(btn==btnplay){ isplayorstop = !isplayorstop; }else if(btn==btnlast){ index--; if(index<0){ index = strsongnames.length-1; } //isplayorstop=true; }else if(btn==btnnext){ index++; index = index%strsongnames.length; }else if(btn==btnloop){ isloop = !isloop; } if(isplayorstop){//播放 playerthread = new thread( new musicrun() ); playerthread.start(); }else{//停止 songslist.setselectedindex(index); songnamelabel.settext("停止播放:"+strsongnames[index]); btnplay.seticon( new imageicon("images2/2.png")); } } } private class musicrun implements runnable{ @override public void run() { currentsong = songs[index]; if(isloop){ currentsong.loop(); songnamelabel.settext("循环播放:"+strsongnames[index]); } if (isplayorstop) { currentsong.play(); } //在播放列表中选定当前歌曲 songslist.setselectedindex(index); //把播放按钮的图标切换成“停止” btnplay.seticon( new imageicon("images2/5.png")); if(!isloop){ songnamelabel.settext("正在播放:"+strsongnames[index]); } } } public static void main(string[] args) { new musicplayer(); } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望通过本文能帮助到大家,谢谢大家对本站的支持!