JAVA swing 怎么做一个按钮能播放mp3.
3个回答
展开全部
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);
}
});
}
}
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);
}
});
}
}
展开全部
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);
}
});
}
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);
}
});
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先初始化一个JButton对象:JButton btn = new JButton("play");
其次添加事件:
btn.addActionListener(new ActionListener(){
});
再次调用外部jar,进行播放MP3。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询