声音在Eclipse中无法播放

huangapple go评论99阅读模式
英文:

Sound not playing in Eclipse

问题

这是类SoundTest的代码:

  1. package code;
  2. import java.io.File;
  3. import javax.sound.sampled.AudioInputStream;
  4. import javax.sound.sampled.AudioSystem;
  5. import javax.sound.sampled.Clip;
  6. public class SoundTest {
  7. public static void main(String args[]) {
  8. File sound = new File("main//Sounds//open1.wav");
  9. playSound(sound);
  10. }
  11. public static void playSound(File f) {
  12. try {
  13. File file = f;
  14. if (file.exists()) {
  15. AudioInputStream audio = AudioSystem.getAudioInputStream(file);
  16. Clip clip = AudioSystem.getClip();
  17. clip.open(audio);
  18. clip.start();
  19. // clip.close();
  20. } else {
  21. System.out.println("无法找到文件");
  22. }
  23. } catch (Exception e) {
  24. System.out.println("未成功工作!");
  25. }
  26. }
  27. }

当我运行它时,没有错误或异常,它从未显示“无法找到文件”或“未成功工作!”。但是我听不到声音。我是否需要在Eclipse中更改某种设置以使声音可听,还是我做错了什么?我相当确定声音文件位于正确的位置。

英文:

Here is the code for class SoundTest:


  1. package code;
  2. import java.io.File;
  3. import javax.sound.sampled.AudioInputStream;
  4. import javax.sound.sampled.AudioSystem;
  5. import javax.sound.sampled.Clip;
  6. public class SoundTest {
  7. public static void main(String args[])
  8. {
  9. File sound = new File("main//Sounds//open1.wav");
  10. playSound(sound);
  11. }
  12. public static void playSound(File f)
  13. {
  14. try {
  15. File file = f;
  16. if(file.exists())
  17. {
  18. AudioInputStream audio = AudioSystem.getAudioInputStream(file);
  19. Clip clip = AudioSystem.getClip();
  20. clip.open(audio);
  21. clip.start();
  22. //clip.close();
  23. }
  24. else
  25. {
  26. System.out.println("Can't find file");
  27. }
  28. }
  29. catch(Exception e)
  30. {
  31. System.out.println("Didn't work!");
  32. }
  33. }
  34. }

When I run it, there are no errors or exceptions, and it never says "Can't find file" or "Didn't work!". But I don't hear a sound. Do I have to change some sort of setting in Eclipse to make the sound audible, or am I doing something wrong? I'm pretty sure the sound file is located in the right place.

答案1

得分: 2

这不是 Eclipse 的问题。文件是由 Java 打开并播放的,但甚至在音频剪辑完成播放之前,程序就被终止了 - 这意味着 Java 程序没有等待剪辑完成播放。

你的方法缺少一行代码,只需要添加这一行代码,就可以得到你想要的结果,见下文:

Thread.sleep(clip.getMicrosecondLength() / 1000);

因此,在添加了这行代码之后,你的代码应该如下所示,然后你就能听到音乐的播放:

  1. AudioInputStream audio = AudioSystem.getAudioInputStream(file);
  2. Clip clip = AudioSystem.getClip();
  3. clip.open(audio);
  4. clip.start();
  5. Thread.sleep(clip.getMicrosecondLength() / 1000);
英文:

That is not eclipse problem. File is being opened and played by java, but even before that audio clip completes playing, the program is getting terminated - Meaning java program is not waiting for the clip to complete playing

Your method is missing one single line of code to give you what you want, see below

Thread.sleep(clip.getMicrosecondLength() / 1000);

So, after adding this line your code should look like this and you will hear your music

  1. AudioInputStream audio = AudioSystem.getAudioInputStream(file);
  2. Clip clip = AudioSystem.getClip();
  3. clip.open(audio);
  4. clip.start();
  5. Thread.sleep(clip.getMicrosecondLength() / 1000);

huangapple
  • 本文由 发表于 2020年5月5日 06:42:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61602841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定