这篇文章主要介绍了Java实现的简单音乐播放器功能,涉及java针对多媒体文件相关载入、播放相关操作技巧,需要的朋友可以参考下
本文实例讲述了java实现的简单音乐播放器功能。分享给大家供大家参考,具体如下:
应用名称:java简单的音乐播放器
用到的知识:java gui编程,线程,io
开发环境:win8+eclipse+jdk1.8
功能说明:可以选择内置的音乐文件播放,循环播放,停止。ps:这个播放器只能播放.au .aiff .wav .midi .rfm格式的音频。
效果图:
源代码: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
|