/*我是要用此程序播放一个声音文件。
就是URL不知道怎么写。
我在上面的注释里面已写好了。。
要不帮我把上面的URL部分具体写清楚。我作参考。。
我把我的声音文件的URL在下面写出来了的。。*/
import java.net.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.*;
public class Sound extends JFrame implements ActionListener{
private JButton playMusic=new JButton("play");
    private JButton loopMusic=new JButton("loop");
    private JButton stopMusic=new JButton("stop");
    private JButton playSound=new JButton("play s");
    private JButton stopSound=new JButton("stop s");
    private AudioClip music =null,sound=null;
    public Sound()
    {super("Sound test");
         //下面的URL定位,我的声音文件在  //F:\eclipse\eclipse\workspace\Sharkgame\soundtest\Audio
    try
{String separator=System.getProperty("file.separator");//此处file该是哪个?
String preface="file:"+System.getProperty("soundtest.dir")//此处的file又是哪个?+separator+"Autio"+separator;
music=Applet.newAudioClip(new URL(preface+"s.wav"));
sound=Applet.newAudioClip(new URL(preface+"v.wav"));
}
    catch(MalformedURLException murle)
{System.err.println("Error loading files"+murle);}
    Container c=getContentPane();
    c.setLayout(new FlowLayout());
    c.add(playMusic);playMusic.addActionListener(this);
    c.add(loopMusic);loopMusic.addActionListener(this);
    c.add(stopMusic);stopMusic.addActionListener(this);
    c.add(playSound);playSound.addActionListener(this);
    c.add(stopSound);stopSound.addActionListener(this);
    validate();pack();setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {if(e.getSource()==playMusic)
     music.play();
    else if(e.getSource()==loopMusic)
     music.loop();
    else if(e.getSource()==stopMusic)
     music.stop();
    else if(e.getSource()==playSound)
     sound.play();
    else if(e.getSource()==stopSound)
     sound.stop();
    }
    public static void main(String args[])
    {Sound st=new Sound();}
}

解决方案 »

  1.   

    file.separator...是系统设置的一个参数..
      

  2.   

    能不能帮我把那段代码出来??我那段URL代码有错的。。
      

  3.   

    第二处的file:是一种格式,表示后边文件路径.
    如果分隔符是"\",是不是应该加两个.一个是转义字符.
      

  4.   

    没提示错。就是找不到文件。。
    能够运行。。就是点击PLAY后没反应。
    我以前想的是file,soundtest.dir,该用实际的URL(也就是我上面给出的我的声音文件的URL)。
    不过不知道file,soundtest.dir是什么意思。
    我希望的是你能把我的URL嵌入到我这段代码中。。
      

  5.   

    new URL(preface+"s.wav").getFile();