一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - java音樂(lè)播放器實(shí)現(xiàn)代碼

java音樂(lè)播放器實(shí)現(xiàn)代碼

2020-11-09 15:25laijiawei JAVA教程

這篇文章主要介紹了java音樂(lè)播放器的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java音樂(lè)播放器的具體代碼,供大家參考,具體內(nèi)容如下

這個(gè)是源碼結(jié)構(gòu)介紹

java音樂(lè)播放器實(shí)現(xiàn)代碼

這個(gè)是界面,有點(diǎn)簡(jiǎn)陋,見笑了,但是基本上的東西都有了,沒辦法,沒有美工的程序?qū)懙慕缑?/p>

java音樂(lè)播放器實(shí)現(xiàn)代碼

直接上源代碼Player.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
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
package com.service;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
 
import javax.sound.sampled.*;
import javax.swing.JSlider;
import javax.swing.JTable;
 
import com.list.MusicList;
import com.list.ThreadList;
import com.list.ViewList;
import com.model.Model;
import com.model.Music;
import com.view.View;
 
 
/*"duration"
"author"
"title"
"copyright"
"date"
"comment"*/
public class Player extends Thread{
 
 
 
 private Player p;
 private long time = 0;
 
 Object lock = new Object();//一個(gè)空的對(duì)象,沒什么意義
 
 private boolean paused = false;// 暫停 繼續(xù)
 
 
 public boolean isPaused() {
 return paused;
 }
 
 public void setPaused(boolean paused) {
 this.paused = paused;
 }
 
 private JSlider jSliderPlayProgress;//播放進(jìn)度條
 
 
 private boolean over = false;//開始 結(jié)束
 
 //是否自動(dòng)播放下一曲
 private boolean isNext=true;
 
 
 private Music music;//音樂(lè)
 
 
 AudioInputStream din = null;
 SourceDataLine line=null;
 
 private FloatControl volume = null;
 
 private JSlider jSliderVolume;
 
 public JSlider getjSliderVolume() {
 return jSliderVolume;
 }
 
 public void setjSliderVolume(JSlider jSliderVolume) {
 this.jSliderVolume = jSliderVolume;
 
 
 }
 public Player(JSlider jSliderVolume,JSlider jSliderPlayProgress) {
 super();
 this.jSliderVolume = jSliderVolume;
 this.jSliderPlayProgress=jSliderPlayProgress;
 }
 
 
 public Music getMusic() {
 return music;
 }
 
 public void setMusic(Music music) {
 this.music = music;
 }
 
 public FloatControl getVolume(){
 return volume;
 }
 
 
 
 //播放音樂(lè)
 public void run(){
 
 AudioInputStream in=null;
 
 try {
 
 File file = new File(music.getPath());
 
 //播放不了的歌曲,直接下一首,并且在音樂(lè)列表中刪除
 try {
  in = AudioSystem.getAudioInputStream(file);
 } catch (Exception e) {
 //刪除有點(diǎn)小問(wèn)題
 MusicList.getList().remove(music.getId());
 
 ViewList.getList().get(0).getJt().setModel(new Model());
 
 
 nextmusic();
 }
 
 AudioFormat baseFormat = in.getFormat();
 AudioFormat decodedFormat = new AudioFormat(
  AudioFormat.Encoding.PCM_SIGNED,
  baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
  baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
  false);
 if(baseFormat.getEncoding()==AudioFormat.Encoding.PCM_UNSIGNED || baseFormat.getEncoding()==AudioFormat.Encoding.ULAW ||
  baseFormat.getEncoding()==AudioFormat.Encoding.ALAW || baseFormat.getEncoding()==AudioFormat.Encoding.PCM_SIGNED){
  time=(file.length()*8000000)/((int)(decodedFormat.getSampleRate()*baseFormat.getSampleSizeInBits()));
 }else{
  int bitrate=0;
  if(baseFormat.properties().get("bitrate")!=null){
  //取得播放速度(單位位每秒)
  bitrate=(int)((Integer)(baseFormat.properties().get("bitrate")));
  if(bitrate!=0)
  time=(file.length()*8000000)/bitrate;
  }
  
 }
 
 
 din = AudioSystem.getAudioInputStream(decodedFormat, in);
 DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
 line = (SourceDataLine) AudioSystem.getLine(info);
 line.open();
 setVolume();
 jSliderPlayProgress.setMaximum((int)time);
 jSliderPlayProgress.setValue(0);
 if(line!=null){
 line.open(decodedFormat);
 byte[] data = new byte[4096];
 int nBytesRead;
 
 synchronized (lock) {
 while ((nBytesRead = din.read(data, 0, data.length)) != -1) {
  while (paused) {
  if(line.isRunning()) {
  line.stop();
  System.out.println("暫停");
  }
  try {
  lock.wait();
  System.out.println("等待");
  }
  catch(InterruptedException e) {
  }
  }
  if(!line.isRunning()&&!over) {
  System.out.println("開始播放");
  line.start();
  
  }
  
  if (over&&line.isRunning()) {
  System.out.println("停止播放");
  jSliderPlayProgress.setValue(0);
  isNext=false;
  line.drain();
  line.stop();
  line.close();
  }
  
  jSliderPlayProgress.setValue((int)line.getMicrosecondPosition());
  line.write(data, 0, nBytesRead);
 }
 
 //根據(jù)播放模式選擇下一首歌
 nextmusic();
 }
 
 }
 
 }
 catch(Exception e) {
 e.printStackTrace();
 }
 finally {
 if(din != null) {
 try { din.close(); } catch(IOException e) { }
 }
 }
 }
 
 //設(shè)置播放器滾動(dòng)條
 public void setVolume()
 {
 if(line!=null)
 {
 if(line.isControlSupported(FloatControl.Type.MASTER_GAIN))
 {
 jSliderVolume.setEnabled(true);
 volume= (FloatControl)line.getControl( FloatControl.Type.MASTER_GAIN );
 jSliderVolume.setMinimum((int)volume.getMinimum());
 jSliderVolume.setMaximum((int)volume.getMaximum());
 //jSliderVolume.setValue((int)(volume.getMinimum()+(4*(volume.getMaximum()-volume.getMinimum()))/5));
 volume.setValue((float)(volume.getMinimum()+(4*(volume.getMaximum()-volume.getMinimum()))/5));
 }
 }
 else
 {
 volume=null;
 jSliderVolume.setEnabled(false);
 }
 }
 private void nextmusic() {
 String mode=Setting.getMode();
 if (isNext&&!mode.equals("one")) {//單曲播放就不執(zhí)行
 int nextid=0;//將要播放的id
 int currentid=Integer.parseInt(this.music.getId());
 System.out.println(mode);
 if (mode.equals("default")&&(currentid==MusicList.getList().size()-1)){
 return;
 }
 
 if (mode.equals("rand")) {
  Random random = new Random();
  nextid=Math.abs(random.nextInt())%MusicList.getList().size();
 }else if (mode.equals("onecircle")) {
 nextid=currentid;
 }else if (mode.equals("default")&&!(currentid==MusicList.getList().size()-1)) {
  nextid=currentid+1;
 }else if (mode.equals("morecircle")) {
 
 nextid=(currentid==MusicList.getList().size()-1)?0:currentid+1;
 }
 JTable jTable=ViewList.getList().get(0).getJt();
 if(nextid==0){//第一個(gè)
 
 jTable.setRowSelectionInterval(0,0);
 
 }else {
  jTable.setRowSelectionInterval(nextid-1,nextid);
 }
  this.stopplay();
  ThreadList.getList().clear();
  p=new Player(jSliderVolume,jSliderPlayProgress);
  p.setMusic(MusicList.getList().get(nextid));
  ThreadList.getList().add(p);
  p.start();
 }
 }
 
 
 
 //開始
 public void startplay(){
 over=false;
 }
 
 
 //停止
 
 public void stopplay(){
 over=true;
 }
 
 
 // 暫停
 public void userPressedPause() {
 paused = true;
 }
 
 //繼續(xù)
 public void userPressedPlay() {
 synchronized(lock) {
  paused = false;
  lock.notifyAll();
 }
 
 }
 
 public void Pause(){
 if (paused) {
 synchronized(lock) {
  paused = false;
  lock.notifyAll();
  }
 }else{
 paused = true;
 }
 
 }
 
 
 
 
}

這個(gè)主要是播放音樂(lè)的類,播放,暫停,停止,上一首,下一首都有了 

 View.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
package com.view;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.TableCellRenderer;
 
 
 
import com.list.MusicList;
import com.list.ThreadList;
import com.list.ViewList;
import com.model.Model;
import com.model.Music;
import com.service.Player;
import com.service.Setting;
import com.util.DirInput;
import com.util.FileInput;
import com.util.FileList;
import com.util.List_File;
 
 
public class View extends JFrame implements MouseListener,ActionListener,WindowListener {
 
 private JButton stop, open,del,next,pre;
 private Player p;
 private JPanel[] jPanels;
 private MusicList list;
 //private Long clickTime=0l;
 private JScrollPane jsp;
 private JTable jt;
 private JRootPane j;
 private Model model;
 private JSlider jSliderVolume;
 private JSlider jSliderPlayProgress;
 private FileInput fileinput;
 private DirInput dirInput;
 private JMenuBar jb;
 private JMenu jm;
 private JMenuItem fm,dm;
 private JComboBox jBox;
 
 
 public View(){
 System.out.println(ViewList.getList().size());
 if (ViewList.getList().size()==0) {
 Open();
 }
 }
 
 
 
 private void Open() {
 
 //this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
 
 
 //this.setUndecorated(true);
 //菜單選項(xiàng)
 
 jb=new JMenuBar();
 jm=new JMenu("打開");
 
 fm=new JMenuItem("文件");
 
 dm=new JMenuItem("文件夾");
 
 fm.addActionListener(this);
 dm.addActionListener(this);
 jb.add(jm);
 
 jm.add(fm);
 jm.add(dm);
 
 
 
 //this.setJMenuBar(jb);
 
 
 
 JPanel p1=new JPanel();
 JPanel p2=new JPanel();
 JPanel p3=new JPanel();
 
 //增加菜單選項(xiàng)
 j=new JRootPane();
 
 j.setJMenuBar(jb);
 
 
 p2.add(j);
 
 
 open=new JButton("播放");
 stop=new JButton("停止");
 open.addMouseListener(this);
 stop.addMouseListener(this);
 pre=new JButton("上一首");
 next=new JButton("下一首");
 pre.addMouseListener(this);
 next.addMouseListener(this);
 p1.setLayout(new GridLayout(2,1));
 
 JPanel jPanel2=new JPanel();
 
 jPanel2.add(open);
 jPanel2.add(stop);
 jPanel2.add(pre);
 jPanel2.add(next);
 
 p1.add(jPanel2);
 /*p1.add(open);
 p1.add(stop);
 p1.add(pre);
 p1.add(next);*/
 
 
 del=new JButton("刪除");
 del.addMouseListener(this);
 
 jSliderPlayProgress = new JSlider(); //播放進(jìn)度條
 jSliderPlayProgress.setValue(0);
 jSliderPlayProgress.setEnabled(false);
 jSliderPlayProgress.setPreferredSize(new Dimension(200, 20));
 
 
 p1.add(jSliderPlayProgress);
 
 
 jSliderVolume = new JSlider(); //音量進(jìn)度條
 jSliderVolume.setValue(0);
 //jSliderPlayProgress.setEnabled(false);
 jSliderVolume.setPreferredSize(new Dimension(100, 20));//設(shè)置滾動(dòng)條長(zhǎng)度
 
 
 
 jSliderVolume.addChangeListener(new ChangeListener()
 {
 public void stateChanged(ChangeEvent evt)
 {
  System.out.println(jSliderVolume.getValue());
  if (ThreadList.getList().size()!=0) {
  ThreadList.getList().get(0).getVolume().setValue((float)jSliderVolume.getValue());
  }
  
  
 }
 });
 String[] v={"順序播放","隨機(jī)播放","單曲循環(huán)","列表循環(huán)","單曲播放"};
 
 jBox=new JComboBox(v);
 
 jBox.addActionListener(this);
 
 p2.add(jBox);
 p2.add(del);
 
 
 p2.add(jSliderVolume);
 
 jPanels=new JPanel[list.getList().size()];
 
 for (int i = 0; i < list.getList().size(); i++) {
 
 Music music=list.getList().get(i);
 
 JPanel jPanel=new MyJPanel(music);
 
 
 JLabel jLabel=new JLabel(music.getName(),SwingConstants.CENTER);
 jLabel.setSize(300, 10);
 jLabel.setHorizontalTextPosition(JLabel.CENTER);
 
 jPanel.setBackground(Color.WHITE);
 
 jPanels[i]=jPanel;
 
 jPanel.addMouseListener(this);
 
 jPanel.add(jLabel);
 
 
 
 p3.add(jPanel);
 
 
 }
 
 
 p3.setBackground(Color.WHITE);
 
 p3.setLayout(new GridLayout(10, 1));
 
 p3.setSize(320, 500);
 
 
 this.add(p1,BorderLayout.NORTH);
 this.add(p2,BorderLayout.SOUTH);
 
 
 
 
 
 
 model=new Model(); //添加表
 
 jt=new JTable(model){ // 設(shè)置jtable的單元格為透明的
 public Component prepareRenderer(TableCellRenderer renderer,
  int row, int column) {
  Component c = super.prepareRenderer(renderer, row, column);
  if (c instanceof JComponent) {
  ((JComponent) c).setOpaque(false);
  }
  return c;
  }
  };;
  
 jt.setOpaque(false);
 
 jt.setRowHeight(30);
 jt.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );
 jt.setShowHorizontalLines(false);
 jt.setSelectionBackground(new Color(189,215,238));
 jt.addMouseListener(this);
 
 jsp = new JScrollPane(jt);
 
 jsp.setOpaque(false);
 jsp.getViewport().setOpaque(false);
 
 
 //addmusic();
 
 //this.add(p3,BorderLayout.CENTER);
 
 this.add(jsp,BorderLayout.CENTER);
 
 
 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 
 this.addWindowListener(this);
 
 Image image=this.getToolkit().getImage("img/icon.jpg");
 
 this.setIconImage(image);
 
 this.setTitle("音樂(lè)播放器");
 
 ImageIcon icon = new ImageIcon("img/bg.jpg");
 JLabel lab = new JLabel(icon); // 將圖片放入到label中
 lab.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight()); // 設(shè)置放有圖片的label的位置
 
 this.getContentPane().add(lab, -1); // jthis本身是窗體,不能放置任何組件,用getContentPane()方法得到this的默認(rèn)內(nèi)容面板,將lab放入其中,-1表示放入面板的下層
 this.getContentPane().add(jsp, 0); // 0表示放在面板的最頂層
 Container con = this.getContentPane();
 ((JPanel)con).setOpaque(false); // 設(shè)置面板為透明的
 p2.setOpaque(false);
 jSliderVolume.setOpaque(false);
 jPanel2.setOpaque(false);
 p1.setOpaque(false);
 p3.setOpaque(false);
 jSliderPlayProgress.setOpaque(false);
 this.setLocation(400, 200);
 this.setSize(337, 525);
 this.setResizable(false);
 this.setVisible(true);
 }
 
 @Override
 public void mouseClicked(MouseEvent e) {
 
 System.out.println("開始播放");
 
 if (e.getSource()==open) {
 
 if (p==null) {//開始
 p=new Player(jSliderVolume,jSliderPlayProgress);
 p.setMusic(MusicList.getList().get(0));
 jt.setRowSelectionInterval(0,0);
 ThreadList.add(p);
 open.setText("暫停");
 p.start();
 }else{//繼續(xù)
 if (ThreadList.getList().size()!=0) {
  p=ThreadList.getList().get(0);
 }
 
 String s=p.isPaused()?"暫停":"播放";
 open.setText(s);
 p.Pause();
 }
 
 }else if (e.getSource()==stop) {
 if (ThreadList.getList().size()!=0) {
 p=ThreadList.getList().get(0);
 }
 if (p!=null) {
 p.stopplay();
 p=null;
 open.setText("播放");
 }
 
 
 }else if (e.getSource()==pre) {//上一首
 premusic();
 
 }else if (e.getSource()==next) {//下一首
 nextmusic();
 }else if (e.getSource()==del) {
 
 delmusic();
  
 }else if (e.getSource()==jt&&e.getClickCount()==2) {//雙擊
 
 clickmusic();
 
 
 
 }
 
 
 }
 
 private void clickmusic() {
 //雙擊Jtable
 System.out.println("點(diǎn)擊了");
 
 int rowNum = this.jt.getSelectedRow();
 System.out.println(rowNum);
 if(rowNum == -1) {
 JOptionPane.showMessageDialog(this, "你沒有選擇一項(xiàng)");
 return;
 }
 ArrayList<Player> list=ThreadList.getList();
 
 
 System.out.println(list.size()+"音樂(lè)文件數(shù)目");
 
 if (list.size()==0) {
 
 p=new Player(jSliderVolume,jSliderPlayProgress);
 p.setMusic(MusicList.getList().get(rowNum));
 ThreadList.add(p);
 open.setText("暫停");
 p.start();
 }else{
 System.out.println("停止");
 list.get(0).stopplay();
 list.clear();
 p=new Player(jSliderVolume,jSliderPlayProgress);
 p.setMusic(MusicList.getList().get(rowNum));
 open.setText("暫停");
 list.add(p);
 p.start();
 }
 }
 
 private void delmusic() {
 int rowNum = this.jt.getSelectedRow();
 
 
 MusicList.getList().remove(rowNum);
 
 System.out.println(MusicList.getList().size());
 
 jt.setModel(new Model());
 
 
 ArrayList<Player> list=ThreadList.getList();
 p=new Player(jSliderVolume,jSliderPlayProgress);
 System.out.println(list.size()+"大小");
 if (list.size()!=0) {
 list.get(0).stopplay();
 list.clear();
 open.setText("暫停");
 if(rowNum==0){//第一個(gè)
  System.out.println("第一個(gè)");
  jt.setRowSelectionInterval(0,0);
  p.setMusic(MusicList.getList().get(rowNum));
  
  
 }else if(rowNum==MusicList.getList().size()){//最后一個(gè)
  System.out.println("最后一個(gè)");
  
  jt.setRowSelectionInterval(rowNum-2,rowNum-1);
  p.setMusic(MusicList.getList().get(rowNum-1));
  
 }else {
  System.out.println("中間");
  
  jt.setRowSelectionInterval(rowNum-1,rowNum);
  p.setMusic(MusicList.getList().get(rowNum));
  
 }
  list.add(p);
  p.start();
 }
 }
 
 public JTable getJt() {
 return jt;
 }
 
 private void premusic() {
 System.out.println("上一首");
 
 ArrayList<Player> list=ThreadList.getList();
 
 int id=Integer.parseInt(list.get(0).getMusic().getId());
 
 if(id!=0){
 if (id==1) {
 jt.setRowSelectionInterval(0,0);
 }else{
 jt.setRowSelectionInterval(id-2,id-1);
 }
 System.out.println(id);
 
 list.get(0).stopplay();
 list.clear();
 
 p=new Player(jSliderVolume,jSliderPlayProgress);
 
 p.setMusic(MusicList.getList().get(id-1));
 System.out.println(id-1);
 
 open.setText("暫停");
 list.add(p);
 p.start();
 }
 }
 
 private void nextmusic() {
 System.out.println("下一首");
 ArrayList<Player> list=ThreadList.getList();
 int id=Integer.parseInt(list.get(0).getMusic().getId());
 
 System.out.println(id);
 if(id!=MusicList.getList().size()-1){ //122
 
 jt.setRowSelectionInterval(id,id+1); //123條
 
 list.get(0).stopplay();
 list.clear();
 
 p=new Player(jSliderVolume,jSliderPlayProgress);
 
 p.setMusic(MusicList.getList().get(id+1));
 System.out.println(id+1);
 
 open.setText("暫停");
 list.add(p);
 p.start();
}
 }
 //判斷雙擊
/* private boolean checkClickTime() {
 long nowTime = (new Date()).getTime();
 if ((nowTime - clickTime) < 300) {
 clickTime = nowTime;
 return true;
 }
 clickTime = nowTime;
 return false;
 }*/
 
 private void addmusic(String path) {//增加mp3文件夾
 
 System.out.println("增加mp3文件夾");
 ArrayList<Music> musiclist=MusicList.getList();
 
 List_File fm = new List_File();
 ArrayList<String[]> FileList = fm.serachFiles(path);
 
 for (int i = 0; i < FileList.size(); i++) {
 Music music= new Music();
  music.setId(musiclist.size()+"");
  String[] s=(String[]) FileList.get(i);
  
  music.setName(s[0]);
  music.setPath(s[1]);
 musiclist.add(music);
 }
 
 jt.setModel(new Model());
 
 }
 
 @Override
 public void mouseEntered(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mouseExited(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mousePressed(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mouseReleased(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 
 
 @Override
 public void actionPerformed(ActionEvent e) {
 if (e.getSource()==fm) {//添加mp3文件
 if(fileinput==null) fileinput = new FileInput(this);
 fileinput.open();
 File[] s=fileinput.getFiles();
 ArrayList<Music> musiclist=MusicList.getList();
 
 
 if(s!=null){
 for(int i=0;i<s.length;i++){
 Music music= new Music();
 music.setId(musiclist.size()+"");
 music.setName(s[i].getName());
 music.setPath(s[i].getAbsolutePath());
 musiclist.add(music);
 jt.setModel(new Model());
 }
 }
 }else if (e.getSource()==dm) {
 
 
 if(dirInput==null) dirInput = new DirInput(this);
 
 dirInput.open();
 
 File s=dirInput.getFile();
 
 
 if(s!=null){
  
  addmusic(s.getAbsolutePath());
 }
 
 
 
 }else if (e.getSource()==jBox) {
 //順序播放 (默認(rèn))default 隨機(jī)rand 單曲循環(huán) onecircle 列表循環(huán) morecircle 單曲播放 one
 
 
 if (ThreadList.getList().size()!=0) {
 p=ThreadList.getList().get(0);
 }else {
 p=new Player(jSliderVolume,jSliderPlayProgress);
 ThreadList.getList().add(p);
 }
 
 String[] s={"default","rand","onecircle","morecircle","one"};
 
 Setting.setMode(s[jBox.getSelectedIndex()]);
 
 
 }
 }
 
 
 
 @Override
 public void windowActivated(WindowEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 
 
 @Override
 public void windowClosed(WindowEvent e) {
 // TODO Auto-generated method stub
 System.out.println("關(guān)閉kk");
 }
 
 
 
 @Override
 public void windowClosing(WindowEvent e) {
 // TODO Auto-generated method stub
 System.out.println("close");
 
 if (MusicList.getList().size()!=0) {
 System.out.println("寫入文件");
 //清空之前的內(nèi)容
 FileList.clear("file/musiclist.txt");
 ArrayList<Music> list=MusicList.getList();
 for (int i = 0; i < list.size(); i++) {
 FileList.writeFile("file/musiclist.txt",list.get(i).getId()+","+list.get(i).getName()+","
  +list.get(i).getPath()+"\n");
 }
 
 }
 
 
 
 }
 
 
 @Override
 public void windowDeactivated(WindowEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 
 @Override
 public void windowDeiconified(WindowEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 
 @Override
 public void windowIconified(WindowEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 
 @Override
 public void windowOpened(WindowEvent e) {
 // TODO Auto-generated method stub
 System.out.println("open");
 
 File file=new File("file/musiclist.txt");
 
 if (file.exists()==false) {
 try {
 file.createNewFile();
 } catch (IOException e1) {
 // TODO Auto-generated catch block
 e1.printStackTrace();
 }
 }else {
 
 
 FileList.readFileByLines("file/musiclist.txt");
 jt.setModel(new Model());
 }
 
 
 }
 
}

剩下的代碼在后面附件上有,現(xiàn)在主要是有幾個(gè)小問(wèn)題,第一,我還沒有找到獲取音樂(lè)文件具體信息比較好的辦法,所以每一首暫時(shí)還沒有歌手,作曲的信息,第二界面有點(diǎn)難看,見諒了,第三個(gè)是打包成jar文件會(huì)有路徑問(wèn)題,暫時(shí)還沒辦法解決,我是直接在myeclipse上運(yùn)行,一切正常,就是打包有點(diǎn)小問(wèn)題

暫時(shí)先說(shuō)這些了,這個(gè)是我業(yè)余時(shí)間的項(xiàng)目,有什么不足的,大家都可以提出來(lái)。

下載地址:音樂(lè)播放器

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲+欧美+国产+综合 | 日本高h| 好大好深好涨好烫还要 | 2012在线观看免费视频大全 | 青青草视频国产 | 青春草视频在线免费观看 | 天天草天天 | 高h辣文小说网 烧书阁 | 含羞草传媒一天免费看下 | 久久99精品久久久久久园产越南 | 久久精品热在线观看30 | 公妇仑乱在线观看 | 99 久久99久久精品免观看 | 日韩免费在线看 | 免费波多野结衣庭教师 | 三级黄色片在线观看 | 亚洲国产成人精品无码区99 | 爱爱一级视频 | 国产精自产拍久久久久久 | 俄罗斯女人与公拘i交酡 | 国产欧美日韩精品一区二 | 国产精品久久久久久久久免费hd | 四虎影院永久网址 | 色呦呦tv | 久久99精品久久久久久园产越南 | 草草影院在线 | 五月桃花网婷婷亚洲综合 | 亚洲国产在线视频精品 | 国产一区二区三区在线 | 国产高清在线不卡 | 久久99影院 | 国产精品成人一区二区1 | 91制片厂果冻传媒杨柳作品 | 精品在线免费播放 | 欧美高清一级 | 亚洲天堂精品在线观看 | 色老板视频在线 | 日本一本草久p | 国产99在线观看 | 91视频无限看 | 999资源站 |