Java实现的简单音乐播放器功能示例

时间:2022-01-25 22:48:20

本文实例讲述了java实现的简单音乐播放器功能。分享给大家供大家参考,具体如下:

应用名称:java简单的音乐播放器

用到的知识:java gui编程,线程,io

开发环境:win8+eclipse+jdk1.8

功能说明:可以选择内置的音乐文件播放,循环播放,停止。ps:这个播放器只能播放.au .aiff .wav .midi .rfm格式的音频。

效果图:

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
import java.applet.audioclip;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.jbutton;
import javax.swing.jcombobox;
import javax.swing.jdialog;
public class musicplayer extends jdialog implements runnable,itemlistener,actionlistener{
  /**
   *
   */
  private static final long serialversionuid = 1l;
  thread thread;
  jcombobox<serializable> choicemusic;
  audioclip clip;
  jbutton buttonplay,buttonloop,buttonstop;
  string str;
  musicplayer(){
    thread=new thread(this);
    choicemusic=new jcombobox();
    choicemusic.additem("选择音频文件");
    choicemusic.additem("src/1.wav");
    choicemusic.additem("‪2.wav");
    choicemusic.additem("3.wav");
    choicemusic.additemlistener(this);
    buttonplay=new jbutton("播放");
    buttonloop=new jbutton("循环");
    buttonstop=new jbutton("停止");
    buttonplay.addactionlistener(this);
    buttonloop.addactionlistener(this);
    buttonstop.addactionlistener(this);
    setlayout(new flowlayout());
    add(choicemusic);
    add(buttonplay);
    add(buttonloop);
    add(buttonstop);
    setdefaultcloseoperation(jframe.dispose_on_close);
    setsize(350,80);
  }
  @override
  public void actionperformed(actionevent e) {
    if(e.getsource()==buttonplay)
      clip.play();
    else if(e.getsource()==buttonloop)
      clip.loop();
    else if(e.getsource()==buttonstop)
      clip.stop();
  }
  @override
  public void itemstatechanged(itemevent arg0) {
    str=choicemusic.getselecteditem().tostring();
    if(!(thread.isalive())){
      thread=new thread(this);
    }
    try{thread.start();
    }
    catch(exception ee){}
  }
  @override
  public void run() {
    try {
    file file=new file(str);
    uri uri=file.touri();
    url url=uri.tourl();
    clip=applet.newaudioclip(url);
    } catch (malformedurlexception e) {
    }
  }
  public static void main(string[] args) {
    musicplayer musicplayer=new musicplayer();
    musicplayer.setvisible(true);
  }
}

希望本文所述对大家java程序设计有所帮助。

原文链接:https://blog.csdn.net/C_jian/article/details/50509044