服务器之家

服务器之家 > 正文

java 实现音乐播放器的简单实例

时间:2021-01-08 11:39     来源/作者:海那边的小萌男

java 实现音乐播放器的简单实例

实现效果图:

java 实现音乐播放器的简单实例

代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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();
  }
 
}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望通过本文能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/weixin_37720904/article/details/71743170

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部