前面我们可以播放声音文件了,但是不能自己控制。
下面的2个程序让我们实现自己控制播放、暂停。
程序1用Button组件实现,程序2用JButton组件实现。
--------------------程序1开始--------------------
Music.java
import java.applet.*;
import java.awt.*;
public class Music extends Applet {
Button play,loop,stop;
AudioClip audio = null;
public void init() {
resize(200,30);
play = new Button("play");
loop = new Button("Loop");
stop = new Button("Stop");
stop.disable();
add(play);
add(loop);
add(stop);
audio = getAudioClip(getCodeBase(),"mid/fy.mid");
}
public boolean action(Event event,Object arg) {
if(event.target == play) {
if(audio!=null) {
audio.play();
play.disable();
loop.disable();
stop.enable();
showStatus("playing sound only once!");
}else
showStatus("Sound file no loaded");
return true;
}else if(event.target == loop) {
if(audio!=null) {
audio.loop();
play.disable();
loop.disable();
stop.enable();
showStatus("Playing sound all the time!");
}else
showStatus("Sound file not loaded");
return true;
}else if(event.target == stop) {
audio.stop();
loop.enable();
stop.disable();
showStatus("Stop playing sound!");
return true;
}
return false;
}
}
-----------------程序2开始------------------
Music.java
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Music extends Applet {
JButton play,loop,stop;
AudioClip audio = null;
public void init() {
resize(200,30);
play = new JButton("play");
loop = new JButton("Loop");
stop = new JButton("Stop");
stop.setEnabled(false);
audio = getAudioClip(getCodeBase(),"mid/fy.mid");
add(play);
play.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
playActionPerformed(event);
}
}
);
add(loop);
loop.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
loopActionPerformed(event);
}
}
);
add(stop);
stop.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
stopActionPerformed(event);
}
}
);
}
private void playActionPerformed(ActionEvent event) {
if(audio!=null) {
audio.play();
play.setEnabled(false);
loop.setEnabled(false);
stop.setEnabled(true);
showStatus("playing sound only once!");
}else
showStatus("Sound file no loaded");
}
private void loopActionPerformed(ActionEvent event) {
if(audio!=null) {
audio.loop();
play.setEnabled(false);
loop.setEnabled(false);
stop.setEnabled(true);
showStatus("Playing sound all the time!");
}else
showStatus("Sound file not loaded");
}
private void stopActionPerformed(ActionEvent event) {
audio.stop();
loop.setEnabled(true);
stop.setEnabled(false);
showStatus("Stop playing sound!");
}
}
------------------程序2结束---------------------
下面的2个程序让我们实现自己控制播放、暂停。
程序1用Button组件实现,程序2用JButton组件实现。
--------------------程序1开始--------------------
Music.java
import java.applet.*;
import java.awt.*;
public class Music extends Applet {
Button play,loop,stop;
AudioClip audio = null;
public void init() {
resize(200,30);
play = new Button("play");
loop = new Button("Loop");
stop = new Button("Stop");
stop.disable();
add(play);
add(loop);
add(stop);
audio = getAudioClip(getCodeBase(),"mid/fy.mid");
}
public boolean action(Event event,Object arg) {
if(event.target == play) {
if(audio!=null) {
audio.play();
play.disable();
loop.disable();
stop.enable();
showStatus("playing sound only once!");
}else
showStatus("Sound file no loaded");
return true;
}else if(event.target == loop) {
if(audio!=null) {
audio.loop();
play.disable();
loop.disable();
stop.enable();
showStatus("Playing sound all the time!");
}else
showStatus("Sound file not loaded");
return true;
}else if(event.target == stop) {
audio.stop();
loop.enable();
stop.disable();
showStatus("Stop playing sound!");
return true;
}
return false;
}
}
-----------------程序2开始------------------
Music.java
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Music extends Applet {
JButton play,loop,stop;
AudioClip audio = null;
public void init() {
resize(200,30);
play = new JButton("play");
loop = new JButton("Loop");
stop = new JButton("Stop");
stop.setEnabled(false);
audio = getAudioClip(getCodeBase(),"mid/fy.mid");
add(play);
play.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
playActionPerformed(event);
}
}
);
add(loop);
loop.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
loopActionPerformed(event);
}
}
);
add(stop);
stop.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
stopActionPerformed(event);
}
}
);
}
private void playActionPerformed(ActionEvent event) {
if(audio!=null) {
audio.play();
play.setEnabled(false);
loop.setEnabled(false);
stop.setEnabled(true);
showStatus("playing sound only once!");
}else
showStatus("Sound file no loaded");
}
private void loopActionPerformed(ActionEvent event) {
if(audio!=null) {
audio.loop();
play.setEnabled(false);
loop.setEnabled(false);
stop.setEnabled(true);
showStatus("Playing sound all the time!");
}else
showStatus("Sound file not loaded");
}
private void stopActionPerformed(ActionEvent event) {
audio.stop();
loop.setEnabled(true);
stop.setEnabled(false);
showStatus("Stop playing sound!");
}
}
------------------程序2结束---------------------
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/213/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
评论列表