java 声音播放问题

来源:百度知道 编辑:UC知道 时间:2024/05/26 06:51:58
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;

public class PlaySound extends Applet
{
String filename = "gong.wav";
AudioInputStream sample;

private URL getURL(String filename)
{
URL url = null;
try
{
url = this.getClass().getResource(filename);
}
catch (Exception e)
{
e.printStackTrace();
}

return url;
}

//Initialize the applet
public void init()
{
try
{
sample = AudioSystem.getAudioInputStream(getURL(filename));
Clip clip=AudioSystem.getClip();
clip.open(sample);
clip.start();
}
catch (MalformedURLException e) { }
catch (IOE

应该是找不到filename
你确定你有"gong.wav"; 这个文件!要注意文件的路径,
最好写绝对路径,否则先测试下默认路径在哪,然后把文件放到这个路径下!
测试方法!在主函数里加上这个就知道路径了!
File file =new File("test.txt");
System.out.println(file.getAbsolutePath());

String path=Parameter.class.getResource("").toString();
你把这个String打出来,自己根据规则进行切割!

这个问题不清楚。