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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|

服務器之家 - 編程語言 - JAVA教程 - 利用java制作簡單的音樂播放器

利用java制作簡單的音樂播放器

2020-11-09 15:28StrictModel JAVA教程

這篇文章主要為大家詳細介紹了利用java的swing技術制作簡單的音樂播放器,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文主要是用到java中的swing技術,以及JMFjar中的API,為大家分享了java音樂播放器的具體實現代碼,供大家參考,具體內容如下

備注:需要用JDK1.8才能播放音樂MP3

?
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
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;
}
 
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: caoporm碰最新免费公开视频 | 精品久久一 | 吃瓜视频在线观看 | 欧美破苞合集 magnet | 我的绝色岳每雯雯 | 色综合九九 | 国产成人一区二区三区小说 | 欧美一级在线播放 | 免费毛片| 丝袜老师好湿好紧我要进去了 | 爽好舒服使劲添高h视频 | 日本视频免费看 | 婷婷久久综合九色综合九七 | 免费理伦片手机在线播放 | 日本老师动漫 | 男女啪啪gif| 九九热视频免费 | 欧美一级在线全免费 | 日本老妇乱子伦中文视频 | 国产精品日韩欧美在线 | 国产日本久久久久久久久婷婷 | 无码人妻丰满熟妇啪啪网不卡 | 日韩欧美亚洲天堂 | 欧美午夜精品 | 99福利视频导航 | 成人在线免费观看视频 | 特黄特色一级aa毛片免费观看 | 天天做日日做 | 男人久久天堂 | 12一14性xxxxx国外 | 亚洲四虎在线 | 四虎在线网址 | 精品91自产拍在线观看99re | 我与白丝同桌的故事h文 | 男人躁女人p | 三级黄色片在线观看 | 99任你躁精品视频 | 423hk四虎 | 青青青青青国产费线在线观看 | 日韩精品亚洲专区在线影视 | 99国产精品久久久久久久... |