看个例子吧
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(),"你的音频对象 au wav midi 等格式");
//在线程thread中创建音频对象。
text.setText("请稍等...");
if(clip!=null)
{button_play.setBackground(Color.green);
button_play.setBackground(Color.green);
text.setText("您可以播放了");
}//当获得音频对象后,通知客户可以播放了。
}
}
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(),"你的音频对象 au wav midi 等格式");
//在线程thread中创建音频对象。
text.setText("请稍等...");
if(clip!=null)
{button_play.setBackground(Color.green);
button_play.setBackground(Color.green);
text.setText("您可以播放了");
}//当获得音频对象后,通知客户可以播放了。
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货