我用如下的程序实现一个音乐播放的功能,不想用applet,为何无法实现停止音乐播放,用stop()无法停止。
importjava.awt.*;importjava.awt.event.*;importjava.io.*;importsun.audio.*;publicclass...
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import sun.audio.*;
public class MyFrame extends Frame implements ActionListener{
Button begin=new Button("开始播放");
Button stop=new Button("停止播放");
Button next=new Button("下一首");
Button last=new Button("上一首");
public MyFrame(String str){
super(str);
setLayout(new BorderLayout());
Panel p=new Panel();
p.add(begin);
p.add(stop);
p.add(next);
p.add(last);
begin.addActionListener(this);
stop.addActionListener(this);
next.addActionListener(this);
last.addActionListener(this);
add("South",p);
setSize(800,600);
setVisible(true);
}
public static void main(String args[]){
new MyFrame("Flash音乐播放器");
}
private static AudioStream as;
public void actionPerformed(ActionEvent evn){
//以下六行只是暂行一试而已
//as=get();
Object obj=evn.getSource();
try{FileInputStream fileau=new FileInputStream("jishiben.wav");
as=new AudioStream(fileau);}
catch(Exception e) {System.out.println("Error");}
if(obj==begin) {
System.out.println("nihao");
play();//AudioPlayer.player.start(as);
//AudioPlayer.player.stop(as);
}
else if(obj==stop){
System.out.println("nibuhao");
stop();//AudioPlayer.player.start(as);
}
}
//用函数来实现播放和停止
public void play(){
if(as==null) {
System.out.println("AudioStream object is not created!");
return;
} else {
AudioPlayer.player.start(as);}
}
public void stop() {
if(as == null) {
System.out.println("AudioStream object is not created!");
return;
} else {
AudioPlayer.player.stop(as);
}
} 展开
import java.awt.event.*;
import java.io.*;
import sun.audio.*;
public class MyFrame extends Frame implements ActionListener{
Button begin=new Button("开始播放");
Button stop=new Button("停止播放");
Button next=new Button("下一首");
Button last=new Button("上一首");
public MyFrame(String str){
super(str);
setLayout(new BorderLayout());
Panel p=new Panel();
p.add(begin);
p.add(stop);
p.add(next);
p.add(last);
begin.addActionListener(this);
stop.addActionListener(this);
next.addActionListener(this);
last.addActionListener(this);
add("South",p);
setSize(800,600);
setVisible(true);
}
public static void main(String args[]){
new MyFrame("Flash音乐播放器");
}
private static AudioStream as;
public void actionPerformed(ActionEvent evn){
//以下六行只是暂行一试而已
//as=get();
Object obj=evn.getSource();
try{FileInputStream fileau=new FileInputStream("jishiben.wav");
as=new AudioStream(fileau);}
catch(Exception e) {System.out.println("Error");}
if(obj==begin) {
System.out.println("nihao");
play();//AudioPlayer.player.start(as);
//AudioPlayer.player.stop(as);
}
else if(obj==stop){
System.out.println("nibuhao");
stop();//AudioPlayer.player.start(as);
}
}
//用函数来实现播放和停止
public void play(){
if(as==null) {
System.out.println("AudioStream object is not created!");
return;
} else {
AudioPlayer.player.start(as);}
}
public void stop() {
if(as == null) {
System.out.println("AudioStream object is not created!");
return;
} else {
AudioPlayer.player.stop(as);
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询