
请教java中,wav文件的播放问题
代码如下,是书上看来的,但是不知道为什么怎么也播放不出来,希望能有人解答~还有,播放的三个音频文件是放在.java的同一个文件夹中吗?classMypYextendsPa...
代码如下,是书上看来的,但是不知道为什么怎么也播放不出来,希望能有人解答~还有,播放的三个音频文件是放在.java的同一个文件夹中吗?
class MypY extends Panel implements Runnable,ItemListener,ActionListener{
Thread thread;
Choice choice;
AudioClip clip;
URL url;
JavaSoundAudioClip player;
Button buttonPlay;//播放按钮
Button buttonLoop;//循环按钮
Button buttonStop;//停止按钮
String str;
MypY(){
thread=new Thread(this);
choice=new Choice();
choice.add("Firstsong.wav");
choice.add("Secondsong.wav");
choice.add("Thirdsong.wav");
choice.addItemListener(this);
buttonPlay=new Button("播放");
buttonLoop=new Button("循环");
buttonStop=new Button("停止");
buttonPlay.addActionListener(this);
buttonLoop.addActionListener(this);
buttonStop.addActionListener(this);
add(choice);
add(buttonPlay);
add(buttonLoop);
add(buttonStop);
str="Firstsong.wav";
}
public void run() {
try{
File file=new File(str);
url=file.toURL();
clip = Applet.newAudioClip(url);
}
catch(Exception e4)
{
System.out.println(e4);
}
}
public void itemStateChanged(ItemEvent arg0) {
str=choice.getSelectedItem();
if(!(thread.isAlive()))
{
thread=new Thread(this);
}
try{
thread.start();
}
catch(Exception ee){}
}
public void actionPerformed(ActionEvent e5) {
if(e5.getSource()==buttonPlay)
{
clip.play();
}
if(e5.getSource()==buttonLoop)
{
clip.loop();
}
if(e5.getSource()==buttonStop)
{
clip.stop();
}
}
}
这只是我建的一个类,main函数在别的类中,但是其他运行一切正常,我放在主要的那个frame中,只有这个播放不了,好像是clip的值是null,好像有说值没传进去的,希望可以得到进一步的解答~ 展开
class MypY extends Panel implements Runnable,ItemListener,ActionListener{
Thread thread;
Choice choice;
AudioClip clip;
URL url;
JavaSoundAudioClip player;
Button buttonPlay;//播放按钮
Button buttonLoop;//循环按钮
Button buttonStop;//停止按钮
String str;
MypY(){
thread=new Thread(this);
choice=new Choice();
choice.add("Firstsong.wav");
choice.add("Secondsong.wav");
choice.add("Thirdsong.wav");
choice.addItemListener(this);
buttonPlay=new Button("播放");
buttonLoop=new Button("循环");
buttonStop=new Button("停止");
buttonPlay.addActionListener(this);
buttonLoop.addActionListener(this);
buttonStop.addActionListener(this);
add(choice);
add(buttonPlay);
add(buttonLoop);
add(buttonStop);
str="Firstsong.wav";
}
public void run() {
try{
File file=new File(str);
url=file.toURL();
clip = Applet.newAudioClip(url);
}
catch(Exception e4)
{
System.out.println(e4);
}
}
public void itemStateChanged(ItemEvent arg0) {
str=choice.getSelectedItem();
if(!(thread.isAlive()))
{
thread=new Thread(this);
}
try{
thread.start();
}
catch(Exception ee){}
}
public void actionPerformed(ActionEvent e5) {
if(e5.getSource()==buttonPlay)
{
clip.play();
}
if(e5.getSource()==buttonLoop)
{
clip.loop();
}
if(e5.getSource()==buttonStop)
{
clip.stop();
}
}
}
这只是我建的一个类,main函数在别的类中,但是其他运行一切正常,我放在主要的那个frame中,只有这个播放不了,好像是clip的值是null,好像有说值没传进去的,希望可以得到进一步的解答~ 展开
3个回答
展开全部
给你一个简单的方法,
import java.applet.*;
import java.io.*;
import java.net.*;
public class AudioPlay {
URL radio = null;
AudioClip clip = null;
File file = null;
public static void main(String[] args) throws MalformedURLException {
new AudioPlay().play("C:\\a.wav");
}
public void play(String path) throws MalformedURLException {
file = new File(path);
radio = file.toURL();
clip = Applet.newAudioClip(radio);
clip.play();
}
}
import java.applet.*;
import java.io.*;
import java.net.*;
public class AudioPlay {
URL radio = null;
AudioClip clip = null;
File file = null;
public static void main(String[] args) throws MalformedURLException {
new AudioPlay().play("C:\\a.wav");
}
public void play(String path) throws MalformedURLException {
file = new File(path);
radio = file.toURL();
clip = Applet.newAudioClip(radio);
clip.play();
}
}
展开全部
这是一个 Panel,当然你写main也看不出结果,你可以把这个 Panel放到一个Frame中运行就可以了
补充~~~~~~~~~~~~~~~~
把异常信息贴出来~
补充~~~~~~~~~~~~~~~~
把异常信息贴出来~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没有main函数啊,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |