JAVA swing 怎么做一个按钮能播放mp3.

 我来答
tangmash
2014-08-14
知道答主
回答量:24
采纳率:0%
帮助的人:12万
展开全部
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;

public class MyPlayer extends Frame implements ActionListener {
AudioStream as = null;
TextField text;
Button button_play, button_loop, button_stop, button_open;
Frame fr = new Frame();
FileDialog d;

public void lanchFrame() {
button_play = new Button("开始播放");
button_loop = new Button("循环播放");
button_stop = new Button("停止播放");
button_open = new Button("打开文件");
text = new TextField(12);
button_play.addActionListener(this);
button_stop.addActionListener(this);
button_loop.addActionListener(this);
this.add(text, BorderLayout.NORTH);
text.setText("请选择音乐");
button_open.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ex) {

d = new FileDialog(fr, "请选择想要播放的背景音乐", FileDialog.LOAD);
d.show();
if (!d.isShowing()) {
File file = new File(d.getDirectory() + d.getFile());
text.setText(d.getFile());
try {
InputStream in = new FileInputStream(file);
as = new AudioStream(in);
} catch (Exception e) {
e.printStackTrace();
}
}
f (as != null) {
button_play.setBackground(Color.red);
button_stop.setBackground(Color.green);
button_loop.setBackground(Color.yellow);
}
}
});
this.add(button_open, BorderLayout.CENTER);
this.add(button_play, BorderLayout.WEST);
this.add(button_loop, BorderLayout.SOUTH);
this.add(button_stop, BorderLayout.EAST);
}

public void actionPerformed(ActionEvent e) {
ContinuousAudioDataStream cas = null;
if (e.getSource() == button_play) {
AudioPlayer.player.start(as);
text.setText("正在播放" + d.getFile());
}
if (e.getSource() == button_loop) {
try {
AudioData data = as.getData();
cas = new ContinuousAudioDataStream(data);
} catch (IOException e1) {
e1.printStackTrace();
}
AudioPlayer.player.start(cas);
}
if (e.getSource() == button_stop) {
if (as != null)
AudioPlayer.player.stop(as);
if (cas != null)
AudioPlayer.player.stop(cas);
text.setText("停止播放");
}
}

public static void main(String[] args) {
MyPlayer myPlayer = new MyPlayer();
myPlayer.lanchFrame();
myPlayer.setSize(200, 100);
myPlayer.setVisible(true);
myPlayer.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}

});

}

}
龙影腾空小学生
2015-06-29 · TA获得超过1700个赞
知道大有可为答主
回答量:1680
采纳率:0%
帮助的人:2355万
展开全部
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;

public class MyPlayer extends Frame implements ActionListener {
AudioStream as = null;
TextField text;
Button button_play, button_loop, button_stop, button_open;
Frame fr = new Frame();
FileDialog d;

public void lanchFrame() {
button_play = new Button("开始播放");
button_loop = new Button("循环播放");
button_stop = new Button("停止播放");
button_open = new Button("打开文件");
text = new TextField(12);
button_play.addActionListener(this);
button_stop.addActionListener(this);
button_loop.addActionListener(this);
this.add(text, BorderLayout.NORTH);
text.setText("请选择音乐");
button_open.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ex) {

d = new FileDialog(fr, "请选择想要播放的背景音乐", FileDialog.LOAD);
d.show();
if (!d.isShowing()) {
File file = new File(d.getDirectory() + d.getFile());
text.setText(d.getFile());
try {
InputStream in = new FileInputStream(file);
as = new AudioStream(in);
} catch (Exception e) {
e.printStackTrace();
}
}
f (as != null) {
button_play.setBackground(Color.red);
button_stop.setBackground(Color.green);
button_loop.setBackground(Color.yellow);
}
}
});
this.add(button_open, BorderLayout.CENTER);
this.add(button_play, BorderLayout.WEST);
this.add(button_loop, BorderLayout.SOUTH);
this.add(button_stop, BorderLayout.EAST);
}

public void actionPerformed(ActionEvent e) {
ContinuousAudioDataStream cas = null;
if (e.getSource() == button_play) {
AudioPlayer.player.start(as);
text.setText("正在播放" + d.getFile());
}
if (e.getSource() == button_loop) {
try {
AudioData data = as.getData();
cas = new ContinuousAudioDataStream(data);
} catch (IOException e1) {
e1.printStackTrace();
}
AudioPlayer.player.start(cas);
}
if (e.getSource() == button_stop) {
if (as != null)
AudioPlayer.player.stop(as);
if (cas != null)
AudioPlayer.player.stop(cas);
text.setText("停止播放");
}
}

public static void main(String[] args) {
MyPlayer myPlayer = new MyPlayer();
myPlayer.lanchFrame();
myPlayer.setSize(200, 100);
myPlayer.setVisible(true);
myPlayer.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}

});

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yugi111
2015-07-10 · TA获得超过8.1万个赞
知道大有可为答主
回答量:5.1万
采纳率:70%
帮助的人:1.3亿
展开全部
  1. 首先初始化一个JButton对象:JButton btn = new JButton("play");

  2. 其次添加事件:

    btn.addActionListener(new ActionListener(){

        

    });

  3. 再次调用外部jar,进行播放MP3。

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式