今天給大家介紹下用java swing開發一款音樂播放器,高仿酷狗音樂播放器,完整源碼地址在最下方,本文只列出部分源碼,因為源碼很多,全部貼不下,下面還是老規矩。來看看運行結果:
下面我們來看看代碼:
首先看一下主窗口的實現代碼:
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
|
package com.baiting; import java.awt.dimension; import java.awt.toolkit; import com.baiting.menu.closewindow; /** * 窗口 * @author lmq * */ public abstract class musicwindow extends music { protected musicframe musicframe; private string title; private int locationx; private int locationy; public musicwindow() { title = getconfigmap().get( "title" ).tostring(); defaultlocation(); } public musicwindow(string title, int width, int height) { this .title = title; setwidth(width); setheight(height); defaultlocation(); } public musicwindow(string title, int width, int height, int locationx, int locationy) { this .title = title; setwidth(width); setheight(height); this .locationx = locationx; this .locationy = locationy; } private void defaultlocation() { dimension screensize = toolkit.getdefaulttoolkit().getscreensize(); locationx = (screensize.width-getwidth())/ 2 ; locationy = (screensize.height-getheight())/ 2 ; } protected musicframe createwindow() { musicframe = new musicframe(); musicframe.settitle(title); musicframe.setsize(getwidth(), getheight()); //musicframe.setlocation(locationx, locationy); musicframe.setlocationrelativeto( null ); musicframe.addwindowlistener( new closewindow()); musicframe.setminimumsize( new dimension( 600 , 450 )); musicframe.setvisible( true ); return musicframe; } public string gettitle() { return title; } public void settitle(string title) { this .title = title; } public int getlocationx() { return locationx; } public void setlocationx( int locationx) { this .locationx = locationx; } public int getlocationy() { return locationy; } public void setlocationy( int locationy) { this .locationy = locationy; } } |
看看在線下載歌曲的代碼:
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
package com.baiting.service; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.file; import java.io.filenotfoundexception; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.util.arraylist; import java.util.list; import com.baiting.bean.downfailsong; import com.baiting.bean.downedsong; import com.baiting.http.downloadsong; import com.baiting.util.stringutil; public class downloadsongservice extends musicservice { private static downloadsongservice instance; private static thread downloadthread; private downloadsongservice() { } public static downloadsongservice getinstance() { if ( null == instance) { instance = new downloadsongservice(); } return instance; } public void startdownload() { if ( null == downloadthread) { downloadthread = new thread( new downloadsong()); downloadthread.start(); } else { if (!downloadthread.isalive()) { downloadthread.interrupt(); downloadthread = null ; downloadthread = new thread( new downloadsong()); downloadthread.start(); } } } public list<downedsong> getdownedsongall() { file downedlistfile = new file(getbasepath()+download_path+ "downed.list" ); if (!downedlistfile.exists()) { try { log.info( "downed.list文件不存在,正在創建...." ); downedlistfile.createnewfile(); log.info( "downed.list文件創建[成功]...." ); } catch (ioexception e) { log.info( "downed.list文件創建[失敗--異常]...." ); e.printstacktrace(); downedlistfile = null ; return null ; } } try { bufferedreader reader = new bufferedreader( new filereader(downedlistfile)); string line = reader.readline(); list<downedsong> list = new arraylist<downedsong>(); int count = 0 ; if (!stringutil.isempty(line)) { while (line != null ) { count++; string content = line.replace( "\n" , "" ).trim(); string[] cols = content.split(separator); if (cols.length> 5 ) { downedsong downedsong = new downedsong(); downedsong.setno(count); downedsong.setfilename(cols[ 0 ].trim()); downedsong.setsongname(cols[ 1 ].trim()); downedsong.setsinger(cols[ 2 ].trim()); downedsong.setfilesize( double .parsedouble(cols[ 3 ].trim())); downedsong.setpath(cols[ 4 ].trim()); downedsong.setcreatetime(cols[ 5 ].trim()); list.add(downedsong); downedsong = null ; } line = reader.readline(); } } reader.close(); reader = null ; if (list.size()> 0 ) { return list; } return null ; } catch (filenotfoundexception e) { log.info( "文件不存在..." ); e.printstacktrace(); return null ; } catch (ioexception e) { e.printstacktrace(); return null ; } finally { downedlistfile = null ; } } /** * 掃描目錄---未完成 * @return */ public list<downedsong> getdownedsongbydir() { string downedsongdir = getdownloadsongpath(); file downeddir = new file(downedsongdir); if (downeddir.exists() && downeddir.isdirectory()) { string[] filelist = downeddir.list(); for ( int i = 0 ; i < filelist.length; i++) { } } return null ; } /** * 判斷歌曲是否存在(通過歌曲名和歌手) * @param songname * @param singer * @return */ public boolean existsongbyinfo(string songname,string singer) { list<downedsong> list = getdownedsongall(); if ( null == list || list.size()< 1 ) { return false ; } boolean flag = false ; for (downedsong downedsong : list) { if (downedsong.getsongname().equals(songname) && downedsong.getsinger().equals(singer)) { flag = true ; break ; } } list = null ; return flag; } /** * 已下載列表中加入新數據 * @param downedsong * @return */ public int adddownedsong(downedsong downedsong) { file downedlistfile = new file(getbasepath()+download_path+ "downed.list" ); if (!downedlistfile.exists()) { try { log.info( "downed.list文件不存在,正在創建...." ); downedlistfile.createnewfile(); log.info( "downed.list文件創建[成功]...." ); } catch (ioexception e) { log.info( "downed.list文件創建[失敗--異常]...." ); e.printstacktrace(); downedlistfile = null ; return - 1 ; } } if ( null != downedsong) { string contents = downedsong.getfilename()+separator+ downedsong.getsongname()+separator+downedsong.getsinger()+separator+ downedsong.getfilesize()+separator+downedsong.getpath()+separator+ downedsong.getcreatetime()+ "\n" ; try { bufferedwriter writer = new bufferedwriter( new filewriter(downedlistfile, true )); writer.write(contents); writer.flush(); writer.close(); writer = null ; list<downedsong> lists = getdownedsongall(); int count = lists.size(); lists = null ; return count; } catch (ioexception e) { log.info(downedlistfile.getname()+ "文件信息寫入[失敗---異常]" ); e.printstacktrace(); return - 1 ; } finally { downedlistfile = null ; downedsong = null ; } } return - 1 ; } /** * 獲取所有下載失敗的歌曲 * @return */ public list<downfailsong> getdownfailsongall() { file downedlistfile = new file(getbasepath()+download_path+ "downfail.list" ); if (!downedlistfile.exists()) { try { log.info( "downfail.list文件不存在,正在創建...." ); downedlistfile.createnewfile(); log.info( "downfail.list文件創建[成功]...." ); } catch (ioexception e) { log.info( "downfail.list文件創建[失敗--異常]...." ); e.printstacktrace(); downedlistfile = null ; return null ; } } try { bufferedreader reader = new bufferedreader( new filereader(downedlistfile)); string line = reader.readline(); list<downfailsong> list = new arraylist<downfailsong>(); int count = 0 ; if (!stringutil.isempty(line)) { while (line != null ) { count++; string content = line.replace( "\n" , "" ).trim(); string[] cols = content.split(separator); if (cols.length> 3 ) { downfailsong failsong = new downfailsong(); failsong.setno(count); failsong.setsongname(cols[ 0 ].trim()); failsong.setsinger(cols[ 1 ].trim()); failsong.setformat(cols[ 2 ].trim()); failsong.setfailtime(cols[ 3 ].trim()); list.add(failsong); } line = reader.readline(); } } reader.close(); reader = null ; if (list.size()> 0 ) { return list; } return null ; } catch (filenotfoundexception e) { log.info( "文件不存在..." ); e.printstacktrace(); return null ; } catch (ioexception e) { e.printstacktrace(); return null ; } catch (exception e) { e.printstacktrace(); return null ; } finally { downedlistfile = null ; } } /** * 已下載列表中加入新數據 * @param downedsong * @return */ public int adddownfailsong(downfailsong downfailsong) { file downfaillistfile = new file(getbasepath()+download_path+ "downfail.list" ); if (!downfaillistfile.exists()) { try { log.info( "downfail.list文件不存在,正在創建...." ); downfaillistfile.createnewfile(); log.info( "downfail.list文件創建[成功]...." ); } catch (ioexception e) { log.info( "downfail.list文件創建[失敗--異常]...." ); e.printstacktrace(); downfailsong = null ; return - 1 ; } } if ( null != downfailsong) { string contents = downfailsong.getsongname()+separator+downfailsong.getsinger()+separator+ downfailsong.getformat()+separator+downfailsong.getfailtime()+ "\n" ; try { bufferedwriter writer = new bufferedwriter( new filewriter(downfaillistfile, true )); writer.write(contents); writer.flush(); writer.close(); list<downfailsong> lists = getdownfailsongall(); return lists.size(); } catch (ioexception e) { log.info(downfaillistfile.getname()+ "文件信息寫入[失敗---異常]" ); e.printstacktrace(); return - 1 ; } catch (exception e) { e.printstacktrace(); return - 1 ; } finally { downfailsong = null ; contents = null ; } } return - 1 ; } /** * 刪除下載失敗的歌曲列表 * @param no * @return */ public boolean deldownfailsong( int no) { list<downfailsong> lists = getdownfailsongall(); if ( null != lists && lists.size()> 0 && lists.size()>=no && no> 0 ) { downfailsong downfailsong = lists.get(no- 1 ); log.info( "刪除下載失敗的歌曲《" +downfailsong.getsongname()+ "》" ); lists.remove(downfailsong); stringbuffer strbuff = new stringbuffer(); if ( null != lists && lists.size()> 0 ) { for (downfailsong fs : lists) { string contents = fs.getsongname()+separator+fs.getsinger()+separator+ fs.getformat()+separator+fs.getfailtime()+ "\n" ; strbuff.append(contents); } } else { strbuff.append( "" ); } file downfaillistfile = new file(getbasepath()+download_path+ "downfail.list" ); if (!downfaillistfile.exists()) { try { log.info( "downfail.list文件不存在,正在創建...." ); downfaillistfile.createnewfile(); log.info( "downfail.list文件創建[成功]...." ); } catch (ioexception e) { log.info( "downfail.list文件創建[失敗--異常]...." ); e.printstacktrace(); return false ; } finally { lists = null ; } } try { bufferedwriter writer = new bufferedwriter( new filewriter(downfaillistfile, false )); writer.write(strbuff.tostring()); writer.flush(); writer.close(); log.info( "刪除下載失敗的歌曲《" +downfailsong.getsongname()+ "》--成功---" ); return true ; } catch (ioexception e) { log.info(downfaillistfile.getname()+ "文件信息寫入[失敗---異常]" ); e.printstacktrace(); return false ; } finally { lists = null ; downfaillistfile = null ; downfailsong = null ; } } return false ; } } |
代碼就貼這么多。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/llqqxf/article/details/51898965