Java 怎么关闭音乐啊 可以成功播放了但是关不掉
在主函数有写两个按钮然后调用这个函数但是只可以播放不能关闭求大神告知importjava.applet.AudioClip;importjava.io.*;importj...
在主函数有写两个按钮 然后调用这个函数但是只可以播放 不能关闭求大神告知
import java.applet.AudioClip;
import java.io.*;
import java.applet.Applet;
import java.awt.Frame;
import java.net.MalformedURLException;
import java.net.URL;
public class Music extends Frame{
public Music(){
super();
}
public void play() {
try {
URL cb;
File f = new File("C:\\Users\\asus\\Workspaces\\MyEclipse Professional 2014\\LianLianKan\\觉醒.wav"); //引号里面的是音乐文件所在的绝对鹿筋
cb = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(cb);
// TODO Auto-generated method stub
aau.play();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void stop() {
try {
URL cb;
File f = new File("C:\\Users\\asus\\Workspaces\\MyEclipse Professional 2014\\LianLianKan\\觉醒.wav"); //引号里面的是音乐文件所在的绝对鹿筋
cb = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(cb);
// TODO Auto-generated method stub
aau.stop();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
} 展开
import java.applet.AudioClip;
import java.io.*;
import java.applet.Applet;
import java.awt.Frame;
import java.net.MalformedURLException;
import java.net.URL;
public class Music extends Frame{
public Music(){
super();
}
public void play() {
try {
URL cb;
File f = new File("C:\\Users\\asus\\Workspaces\\MyEclipse Professional 2014\\LianLianKan\\觉醒.wav"); //引号里面的是音乐文件所在的绝对鹿筋
cb = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(cb);
// TODO Auto-generated method stub
aau.play();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void stop() {
try {
URL cb;
File f = new File("C:\\Users\\asus\\Workspaces\\MyEclipse Professional 2014\\LianLianKan\\觉醒.wav"); //引号里面的是音乐文件所在的绝对鹿筋
cb = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(cb);
// TODO Auto-generated method stub
aau.stop();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
} 展开
展开全部
你在play()和stop()函数分别AudioClip aau = Applet.newAudioClip(cb);造成了两个AudioClip对象,它们之间没有关系,所以,你播放后,没法停止.把AudioClip aau对象作为一个共享的全局变量,就可以播放后,停止了.
更改后的Java程序如下,注意要先点击play按钮,后点击stop按钮,因为只有先播放才能停止.
import java.applet.AudioClip;
import java.io.*;
import java.applet.Applet;
import java.awt.Frame;
import java.net.MalformedURLException;
import java.net.URL;
public class Music extends Frame{
URL cb;
File f = new File("C:\\Users\\asus\\Workspaces\\MyEclipse Professional 2014\\LianLianKan\\觉醒.wav"); //引号里面的是音乐文件所在的绝对鹿筋
AudioClip aau;
public Music(){
super();
}
public void play() {
try {
cb = f.toURL();
aau = Applet.newAudioClip(cb);
// TODO Auto-generated method stub
aau.play();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void stop() {
try {
// TODO Auto-generated method stub
aau.stop();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询