简体   繁体   English

小程序为什么不播放音频剪辑?

[英]Why is the applet not playing the audio clip?

I have an applet that is supposed to make a sound on click but it does not work. 我有一个应在单击时发出声音的小程序,但它不起作用。 It would be really helpful if someone could point out what is wrong with the code 如果有人可以指出代码有什么问题,那将非常有帮助

 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) {} 

I ran your code on my computer and it worked perfectly. 我在计算机上运行了您的代码,它运行良好。 The audio clip stored in soundFile1 starts playing when the applet is run and when the mouse is clicked, the audio that was already playing stops and the new audio clip in soundFile2 starts playing. 运行小应用程序并单击鼠标时,存储在soundFile1中的音频剪辑开始播放,已经播放的音频停止,并且soundFile2中的新音频剪辑开始播放。 I tested the program with a .wav and a .au file. 我使用.wav.au文件测试了该程序。 The problem in your case might be one of the following: 您遇到的问题可能是以下之一:

  1. The audio file is missing from the directory where the .class file of your applet is. 小程序的.class文件所在的目录中缺少音频文件。 The audio file should be present in the directory of the current project you're working on. 音频文件应该存在于您正在处理的当前项目的目录中。

  2. There's something wrong with the .au file that exists in the current directory. 当前目录中的.au文件存在问题。 Test the applet with other file formats (eg. .wav ) or another .au file. 用其他文件格式(例如.wav )或另一个.au文件测试该applet。

  3. Something is wrong with your computer's audio codecs. 您计算机的音频编解码器出了点问题。 Look up on the internet to fix your audio codecs. 在互联网上查找可修复您的音频编解码器。

  4. Try re-installing your JDK. 尝试重新安装您的JDK。 As @Tbtimber said, try using getCodeBase() instead of getDocumentBase() . 正如@Tbtimber所说,尝试使用getCodeBase()而不是getDocumentBase() If it works, then update/re-install your JDK. 如果可行,请更新/重新安装您的JDK。

创建AudioClip时,请尝试使用getCodeBase()代替getDocumentBase()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM