导航:首页 > 编程语言 > javaswing音乐播放器

javaswing音乐播放器

发布时间:2021-02-25 23:10:36

① 谁能帮忙用java做一个播放器

MML是什么?
我做过一个MP3的..给你看看.

package music;
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, qu;

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("删除");
qu = new JButton("读取");
baocun = new JButton("保存");
jp3 = new JPanel();
jp3.add(tjian);
jp3.add(shanc);
jp3.add(baocun);
jp3.add(qu);
c.add(jp3);

try {
String s = "c:\\music.txt";
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);
}
}
});
qu.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 = xu(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.toString());
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 xu(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;
}

}

② 求一个JAVA音乐播放器的源代码

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MediaPlayer extends JFrame implements ActionListener,
ItemListener, ControllerListener {
String title;

Player player;
boolean first = true, loop = false;
Component vc, cc;
String currentDirectory=null;
// 构造函数,其中包括了设置响应窗口事件的监听器。
MediaPlayer(String title) {
super(title);
/* 关闭按钮的实现。。 */
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}

public void windowClosed(WindowEvent e) {
if (player != null)
player.close();
System.exit(0);
}

});
// 调用程序菜单栏的方法成员完成菜单的布置
setupMenu();
setSize(400, 400);
setVisible(true);
}

// 本方法用以设置程序菜单栏
public void setupMenu() {
// 设置一个菜单
Menu f = new Menu("文件");
// 往设置的菜单添加菜单项
MenuItem mi = new MenuItem("打开");
f.add(mi);
mi.addActionListener(this);
f.addSeparator();
CheckboxMenuItem cbmi = new CheckboxMenuItem("循环", false);
cbmi.addActionListener(this);
f.add(cbmi);
f.addSeparator();
MenuItem ee = new MenuItem("退出");
ee.addActionListener(this);
f.add(ee);
f.addSeparator();

Menu l = new Menu("播放列表");
Menu c = new Menu("播放控制");
MenuItem move = new MenuItem("播放");
move.addActionListener(this);
c.add(move);
c.addSeparator();
MenuItem pause = new MenuItem("暂停");
pause.addActionListener(this);
c.add(pause);
c.addSeparator();
MenuItem stop = new MenuItem("停止");
stop.addActionListener(this);
c.add(stop);
c.addSeparator();
// 设置一个菜单栏
MenuBar mb = new MenuBar();
mb.add(f);
mb.add?;
mb.add(l);
// 将构造完成的菜单栏交给当前程序的窗口;
setMenuBar(mb);
}

// 动作时间响应成员;捕捉发送到本对象的各种事件;
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String cufile, selectfile, currentDirectory;
if (e.getActionCommand().equals("退出")) {
// 调用dispose以便执行windowClosed
dispose();
return;
}
// 此事表明拥护选择了“播放”命令;
// 如果当前有一个文件可以播放则执行播放命令;
if (e.getActionCommand().equals("播放")) {
if (player != null) {
player.start();
}
return;
}
// 如果当前正在播放某一文件,则执行暂停;
if (e.getActionCommand().equals("暂停")) {
if (player != null) {
player.stop();
}
return;
}
// 停止命令的响应;
if (e.getActionCommand().equals("停止")) {
if (player != null) {
player.stop();
player.setMediaTime(new Time(0));
}
return;
}
// 用户选择要播放的媒体文件
if (e.getActionCommand().equals("打开")) {
FileDialog fd = new FileDialog(this, "打开媒体文件", FileDialog.LOAD);
// fd.setDirectory(currentDirectory);

2008-2-6 02:46 回复

肆方茉莉
62位粉丝
6楼

fd.setVisible(true);
// 如果用户放弃选择文件,则返回
if (fd.getFile() == null) {
return;
}
// 保存了所选文件的名称及其路径名称已被稍后使用
// 同时设置当前文件夹路径
selectfile = fd.getFile();
currentDirectory = fd.getDirectory();
cufile = currentDirectory + selectfile;
// 将用户选择的文件作为一个菜单项加入播放列表,该菜单项名为该文件名;
// 被点击后给出的命令串是该文件的全路径名
MenuItem mi = new MenuItem(selectfile);
mi.setActionCommand(cufile);
MenuBar mb = getMenuBar();
Menu m = mb.getMenu(2);
mi.addActionListener(this);
m.add(mi);
} else {
// 程序逻辑运行到次表示用户选择了一个“播放列表”中的媒体文件
// 此时可以通过如下动作获得该文件的全路径名
cufile = e.getActionCommand();
selectfile = cufile;
}
// 如果存在一个播放器,则先将其关闭,稍后再重新创建
// 创建播放器时需要捕捉一些异常
if (player != null) {
player.close();
}
try {
player = Manager.createPlayer(new MediaLocator("file:" + cufile));
} catch (Exception e2) {
System.out.println(e2);
return;
}/*
* catch(NoPlayerException e2){ System.out.println("不能找到播放器");
* return ; }
*/
if (player == null) {
System.out.println("无法创建播放器");
return;
}
first = false;
setTitle(selectfile);
// 设置处理播放控制器实际的对象;
/**/
player.addControllerListener(this);
player.prefetch();
}

// 菜单状态改变事件的响应函数;
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub

}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MediaPlayer("播放器");
}

// 调用绘图函数进行界面的绘制 // public void update() {
// }
// 绘图函数成员 //public void paint(Graphics g) {
// }
public void controllerUpdate(ControllerEvent e) {
// TODO Auto-generated method stub
Container tainer = getContentPane();
// 调用player.close()时ControllerClosedEvent事件出现
// 如果存在视觉部件,则该部件应该拆除(为了一致起见,我们对控制面版部件也执行同样的操作,下一次需要时再构造)
if (e instanceof ControllerClosedEvent) {
if (vc != null) {
remove(vc);
vc = null;
}
if (cc != null) {
remove(cc);
cc = null;
}
}

// 播放结束时,将播放指针置于文件之首,如果设定了循环播放,则再次启动播放器;
if (e instanceof EndOfMediaEvent) {
player.setMediaTime(new Time(0));
if (loop) {
player.start();
}
return;
}

// PrefetchCompletEvent事件发生后调用start,正式启动播放
if (e instanceof PrefetchCompleteEvent) {
player.start();
return;
}

// 本事件表示由于播放的资源已经确定;此时要将媒体的图形conmopnent
// 如果有显示出来,同时将播放器player的控制显示到窗口里;
if (e instanceof RealizeCompleteEvent) {
// 如果媒体中有图像,将对应图像component载入窗体;
vc = player.getVisualComponent();
if (vc != null)
tainer.add(vc, BorderLayout.CENTER);
// 将对应控制器component载入窗体;
cc = player.getControlPanelComponent();
cc.setBackground(Color.blue);
if (cc != null)
tainer.add(cc, BorderLayout.SOUTH);
// 有一些特殊媒体在播放时提供另外的控制手段,将控制器一并加入窗口;
/*
* gc=player.getGainControl(); gcc=gc.getControlComponent();
* if(gcc!=null) tainer.add(gcc,BorderLayout.NORTH);
*/
// 根据媒体文件中是否有图像,设定相应的窗口大小
if (vc != null) {
pack();
return;
} else {
setSize(300, 75);
setVisible(true);
return;
}
}

} }

③ 怎样通过java打开音乐播放器

java中打开音乐播放器的方式是使用audioclip类来播放音乐,实例如下:

importjava.applet.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.net.*;

importjavax.swing.*;
importjava.io.File;

{
booleanlooping=false;
Filefile1=newFile("music\明天会更好.wav");
AudioClipsound1;
AudioClipchosenClip;

JButtonplayButton=newJButton("播放");
JButtonloopButton=newJButton("循环播放");
JButtonstopButton=newJButton("停止");
JLabelstatus=newJLabel("选择播放文件");
JPanelcontrolPanel=newJPanel();
Containercontainer=getContentPane();

publicAudioPlayDemo(){
try{
sound1=Applet.newAudioClip(file1.toURL());
chosenClip=sound1;
}catch(OutOfMemoryErrore){
System.out.println("内存溢出");
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}
playButton.addActionListener(this);
loopButton.addActionListener(this);
stopButton.addActionListener(this);
stopButton.setEnabled(false);

controlPanel.add(playButton);
controlPanel.add(loopButton);
controlPanel.add(stopButton);

container.add(controlPanel,BorderLayout.CENTER);
container.add(status,BorderLayout.SOUTH);

setSize(300,130);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
}

publicvoidactionPerformed(ActionEventevent){
if(chosenClip==null){
status.setText("声音未载入");
return;
}
Objectsource=event.getSource();//获取用户洗涤激活的按钮

if(source==playButton){
stopButton.setEnabled(true);
loopButton.setEnabled(true);
chosenClip.play();
status.setText("正在播放");
}

if(source==loopButton){
looping=true;
chosenClip.loop();
loopButton.setEnabled(false);
stopButton.setEnabled(true);
status.setText("正在循环播放");
}
if(source==stopButton){
if(looping){
looping=false;
chosenClip.stop();
loopButton.setEnabled(true);
}else{
chosenClip.stop();
}
stopButton.setEnabled(false);
status.setText("停止播放");
}
}
publicstaticvoidmain(Strings[]){
newAudioPlayDemo();
}
}

只能播放wav格式的歌曲

④ JAVA的SWING中哪个控件可以播放音乐

any class' methods of Swing cannot play sounds.

but you can use:Applet,JMF and Java Sound API(javax.sound.sampled.* and javax.sound.midi.*)

Swing is only GUI components.

⑤ java制作音乐播放器要求实现换肤歌词显示!谁有制作视频急求

Swing窗口可以通过 更改 Look and Feel 来更好主题, 实现皮肤的更换.

LRC文件,可以通过IO流读取到内存专,然后进行解析. 在声音属播放到指定的时间点,显示歌词

⑥ java 音乐播放器

//哈哈昨晚上刚做了一个基本功能是有美化和完善就靠你了,代码如下:
//顺便说一下你电脑里需要有javax.media包才行,没有的话和我说。
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.File;
importjavax.media.Manager;
importjavax.media.MediaLocator;
importjavax.media.Player;
importjavax.swing.AbstractAction;
importjavax.swing.JButton;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
publicclassMusicDemo{
publicstaticvoidmain(Stringargs[])throwsInterruptedException{
MusicPlayermp=newMusicPlayer("音乐播放器");
}
}
{
JButtonbtnOK=newJButton("选歌");
JLabellblCaption=newJLabel("Hello!China!");
JButtonbtnCancel=newJButton("播放");
Playerplay=null;
Stringpath="";
MusicPlayer(Stringtitile){
super(titile);
doublely=Toolkit.getDefaultToolkit().getScreenSize().getHeight();
doublelx=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
setLocation((int)lx/3,(int)ly/3);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,200);
btnOK.setBounds(0,0,80,30);
this.getContentPane().add(btnOK);
lblCaption.setBounds(5,40,120,30);
this.getContentPane().add(lblCaption);
btnCancel.setBounds(120,0,80,30);
this.getContentPane().add(btnCancel);
btnOK.addActionListener(newAbstractAction(){
publicvoidactionPerformed(ActionEvente){
lblCaption.setText("YouClickOK!");
path=open();
}
}
);
btnCancel.addActionListener(newAbstractAction(){
publicvoidactionPerformed(ActionEvente){
lblCaption.setText("YouClickCancel!");
try{
Filefile=newFile(path);
if(play==null){
if(file.exists()){

MediaLocatorlocator=newMediaLocator("file:"+file.getAbsolutePath());
play=Manager.createRealizedPlayer(locator);
play.prefetch();
add(play.getControlPanelComponent(),"South");
play.start();
}
}
}catch(Exceptione1){
e1.printStackTrace();
}

}
});
}
Stringopen(){
StringfilePath="";
JFileChooserfileChooser=newJFileChooser();
fileChooser.setDialogTitle("选择歌...");
//fileChooser.setFileSelectionMo(JFileChooser.DIRECTORIES_ONLY);
intreturnVal=fileChooser.showOpenDialog(fileChooser);
if(returnVal==JFileChooser.APPROVE_OPTION){
filePath=fileChooser.getSelectedFile().getAbsolutePath();
}
returnfilePath;
}
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
}
}

⑦ java中Swing怎么添加音乐

JAVA应用程序添加背景音乐示例:

1.先定义一个 URL 对象,并赋NULL值;

URL musicURL = null;

2.然后为定义的 URL 赋值,即得到要播放的URL地址,

musicURL = new URL( "音乐地址");//得到要播放音乐的url

3.创建一个播放音频的实例,并得到这个实例

AudioClip ac = Applet.newAudioClip(musicURL); //得到一个播放音频的实例

4.进行播放控制

ac.play(); //播放一编

ac.loop(); //循环播放

ac.stop();//停止

播放本地音乐示例:

private void playMusic(){
try {
javax.media.Player player;
if(null==player){
File musicFile=new File("./music.mp3");//得到一个MP3文件
if(musicFile.exists()){
MediaLocator locator=new MediaLocator("file:"+musicFile.getAbsolutePath());
player = Manager.createRealizedPlayer(locator);
player.prefetch();//预读文件
}else{
System.err.println(pro.getMusicFile()+" 找不到");
}
}
//centerpanel.add(player.getControlPanelComponent()!=null?player.getControlPanelComponent():null);//显示播放工具
player.start();//播放
} catch (CannotRealizeException ex) {
ex.printStackTrace();
} catch (NoPlayerException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}

⑧ 单击一个按钮时播放音乐怎么用java swing来实现的

AudioClip sound=Applet.newAudioClip(url)

sound.play();

⑨ java编音乐播放器

package bofan;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.File.*;
import java.io.*;
import javax.media.ControllerClosedEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import java.beans.*;

public class yinyue{
private static String path = "a.mp3";

public static void main(String[] args) {
File f1 = new File(path);
try {
Player player = Manager.createRealizedPlayer(f1.toURI().toURL());
System.out.println("获得文件播放时间: "
+ player.getDuration().getSeconds());
player.prefetch();
player.setMediaTime(new Time(10.0));//从莫个时间段开始播放
player.start();
}catch(Exception ex){}
}
}

⑩ JAVA做音乐播放器简单吗

思路很简单,就是用第三方的java mp3解码器完成文件播放功能,然后用swing组件包装一下就行版。刚好业界权有一个JMF java media framework,是大名鼎鼎的sun开发的。详看这里的官方网页:http://java.sun.com/procts/java-media/jmf/。网上也有很多JMF教程
我曾经做过一个简单的,支持文件列表。这是一个很好的编程训练。

阅读全文

与javaswing音乐播放器相关的资料

热点内容
linux查找特殊字符 浏览:451
linux安装solr 浏览:28
iphone系统文件越用越大 浏览:64
微信修改的文件在哪里设置密码 浏览:48
老版支付宝安卓下载手机版本 浏览:688
动画文件格式为 浏览:180
微信怎么改单 浏览:368
笔记本韩国网站怎么访问 浏览:574
sai透视教程 浏览:346
苹果6s64g的停产了吗 浏览:507
猫眼实时票房榜排名 浏览:339
java大数据云计算方向企业级开发 浏览:718
为什么集搜客没有ms数据台 浏览:672
国内免费在线观看网址 浏览:935
qq原来头像 浏览:805
苹果手机看网站你懂得 浏览:740
立式编带料如何编程 浏览:11
跳过win10安装密钥 浏览:714
能在线看的网站 浏览:220
剑灵模型数据库 浏览:427

友情链接