为什么小程序不播放音频片段?

Why is the applet not playing the audio clip?

我有一个应该在点击时发出声音的小程序,但它不起作用。如果有人能指出代码有什么问题,那将非常有帮助

 AudioClip soundFile1; 
 AudioClip soundFile2;

 public void init()  
 {
      soundFile1 = getAudioClip(getDocumentBase(),"volcanoe.au"); 
      soundFile2 = getAudioClip(getDocumentBase(),"volcanoe.au");
      addMouseListener(this); 
      setBackground(Color.yellow); 
      soundFile1.play(); 
 }

 public void paint(Graphics g)  
 { 
      g.drawString("Click to hear a sound",20,20); 
 }
 public void mouseClicked(MouseEvent evt)  
 { 
      soundFile2.play(); 
 }
 public void mousePressed(MouseEvent evt) {} 
 public void mouseReleased(MouseEvent evt) {} 
 public void mouseEntered(MouseEvent evt) {} 
 public void mouseExited(MouseEvent evt) {} 

创建 AudioClip 时尝试使用 getCodeBase() 而不是 getDocumentBase()

我 运行 你的代码在我的电脑上运行得很好。 soundFile1中存储的音频片段在小程序为运行时开始播放,当单击鼠标时,已经播放的音频停止,[=23中的新音频片段=]soundFile2 开始播放。我用 .wav.au 文件测试了程序。您遇到的问题可能是以下之一:

  1. 您的小程序 .class 文件所在的目录中缺少音频文件。音频文件应该存在于您正在处理的当前项目的目录中。

  2. 当前目录中的 .au 文件有问题。使用其他文件格式(例如 .wav)或其他 .au 文件测试小程序。

  3. 您计算机的音频编解码器出了点问题。在 Internet 上查找以修复您的音频编解码器。

  4. 尝试重新安装您的 JDK。正如 @Tbtimber 所说,尝试使用 getCodeBase() 而不是 getDocumentBase()。如果有效,那么 update/re-install 你的 JDK.