这是耿祥义教材上的例子(还有比这更强的你自己看看吧),绝对满足楼主的要求!
这种方法只能播放au格式的文件,wav的文件可以把后缀改成au也可以播放。而mp3、wma等格式就不能播放。
我也正在找呢你找到好的叫我一声哟!
给我分呀!!!import java.applet.*;import java.awt.*;
import java.awt.event.*;
public class Example24_2 extends Applet implements ActionListener,Runnable
{AudioClip clip;//声明一个音频对象。
TextField text;Thread thread;
Button button_play,button_loop,button_stop;
public void init()
{ thread=new Thread(this);//创建一个新的线程。
thread.setPriority(Thread.MIN_PRIORITY);
button_play=new Button("开始播放"); button_loop=new Button("循环播放");
button_stop=new Button("停止播放"); text=new TextField(12);
button_play.addActionListener(this);
button_stop.addActionListener(this);
button_loop.addActionListener(this);
add(button_play);add(button_loop);add(button_stop);
add(text);
}
public void start()
{thread.start();}
public void stop()
{clip.stop();}//当离开此页面时停止播放。
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button_play)
{ clip.play();}
else if(e.getSource()==button_loop)
{ clip.loop();}
if(e.getSource()==button_stop)
{ clip.stop();}
}
public void run()
{ clip=getAudioClip(getCodeBase(),java中支持的播放类型);
//在线程thread中创建音频对象。
text.setText("请稍等...");
if(clip!=null)
{button_play.setBackground(Color.green);
button_play.setBackground(Color.green);
text.setText("您可以播放了");
}//当获得音频对象后,通知客户可以播放了。
}
}
这种方法只能播放au格式的文件,wav的文件可以把后缀改成au也可以播放。而mp3、wma等格式就不能播放。
我也正在找呢你找到好的叫我一声哟!
给我分呀!!!import java.applet.*;import java.awt.*;
import java.awt.event.*;
public class Example24_2 extends Applet implements ActionListener,Runnable
{AudioClip clip;//声明一个音频对象。
TextField text;Thread thread;
Button button_play,button_loop,button_stop;
public void init()
{ thread=new Thread(this);//创建一个新的线程。
thread.setPriority(Thread.MIN_PRIORITY);
button_play=new Button("开始播放"); button_loop=new Button("循环播放");
button_stop=new Button("停止播放"); text=new TextField(12);
button_play.addActionListener(this);
button_stop.addActionListener(this);
button_loop.addActionListener(this);
add(button_play);add(button_loop);add(button_stop);
add(text);
}
public void start()
{thread.start();}
public void stop()
{clip.stop();}//当离开此页面时停止播放。
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button_play)
{ clip.play();}
else if(e.getSource()==button_loop)
{ clip.loop();}
if(e.getSource()==button_stop)
{ clip.stop();}
}
public void run()
{ clip=getAudioClip(getCodeBase(),java中支持的播放类型);
//在线程thread中创建音频对象。
text.setText("请稍等...");
if(clip!=null)
{button_play.setBackground(Color.green);
button_play.setBackground(Color.green);
text.setText("您可以播放了");
}//当获得音频对象后,通知客户可以播放了。
}
}
解决方案 »
- java 窗口 JDialog和JFrame对象如何让它不能最小化?
- java3d 可否载入 3d 模型 具体方法是?我是rookie
- 组件用不了,不知道是怎么回事
- 急救:ClassCastException--运行时类型和判断是否是某种类实例之间的关系不是一致的么?
- 诚心请教JAVA图形用户界面和IO处理问题!
- 求指导——菜单栏菜单监听器不响应
- 如何设置struts的中文?
- Exception in thread "main" java.lang.NoClassDefFoundError:Ellsworth(这是我的文件)。上面是我编译通过之后,执行是出现的信息?哪里
- 请问我在编译JAVA程序时,为何总是出现Exception in thread "main" java.lang.NoClassDefFoundError的错误
- 工厂设计模式实现时出现错误
- 请问怎么用client通过server连接database sql里面的数据库,是不是要用rmi的方法,可不可以提供一些简单的例子给我?多谢!!另外怎么用m
- 关于字符串和字符的问题
不知道这个对你有没有用哦
http://big5.ccidnet.com:89/gate/big5/tech.ccidnet.com/pub/article/c340_a47658_p1.html
import java.awt.event.*;
import javax.swing.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class MainFrame extends JFrame {
private JPanel contentPane;
private JTextField jTextField1 = new JTextField();
private JLabel jLabel1 = new JLabel();
private JPanel jPanel1 = new JPanel();
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton(); private java.applet.AudioClip clip; //Construct the frame
public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
this.setSize(new Dimension(418, 118));
this.setTitle("JSoundDemo1");
jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
jLabel1.setToolTipText("");
jLabel1.setText("Waiting.........");
jButton1.setFont(new java.awt.Font("Dialog", 0, 12));
jButton1.setText("Play");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setEnabled(false);
jButton2.setFont(new java.awt.Font("Dialog", 0, 12));
jButton2.setText("Stop");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jTextField1.setText(ClassLoader.getSystemResource("sounds/spacemusic.au").toString());
contentPane.add(jLabel1, BorderLayout.SOUTH);
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, null);
jPanel1.add(jButton2, null);
contentPane.add(jTextField1, BorderLayout.NORTH); clip=null;
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void jButton1_actionPerformed(ActionEvent e) {
try{
jButton1.setEnabled(false);
jButton2.setEnabled(true);
clip = java.applet.Applet.newAudioClip(new java.net.URL(this.jTextField1.getText()));
// clip = java.applet.Applet.newAudioClip(ClassLoader.getSystemResource("sounds/spacemusic.au")); clip.play(); this.jLabel1.setText("Now Playing "+this.jTextField1.getText()); }catch(Exception err){
err.printStackTrace();
}
} void jButton2_actionPerformed(ActionEvent e) {
try{
jButton1.setEnabled(true);
jButton2.setEnabled(false);
clip.stop();
this.jLabel1.setText("Waiting .......");
}catch(Exception err){
err.printStackTrace();
}
}
}