1

我正在尝试使用可用的代码:如何在 Java 中播放声音? 但我不能在那里发布问题,因为这是一个新帐户并且只有 1 个声誉。

原始代码:

  public static synchronized void playSound(final String url) {
  new Thread(new Runnable() { // the wrapper thread is unnecessary, unless it blocks on the Clip finishing, see comments
  public void run() {
    try {
      Clip clip = AudioSystem.getClip();
      AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream("/path/to/sounds/" + url));
      clip.open(inputStream);
      clip.start(); 
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
  }
}).start();
}

这是我的代码:

package sound_test;
import javax.sound.sampled.*;

public class Main {

public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
  public void run() {
    try {
      Clip clip = AudioSystem.getClip();
      AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream("/path/to/sounds/" + url));
      clip.open(inputStream);
      clip.start();
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
  }
}).start();
}

public static void main(String[] args) {
    // TODO code application logic here
    playSound("C:\\warning_test.wav");
}

}

当我运行代码时,我收到“null”作为输出并且没有声音出来。我检查了文件名和路径,它是正确的。

截图:

http://puu.sh/pkYo

http://puu.sh/pkZl

先感谢您。

4

2 回答 2

0

你可以做

AudioInputStream inputStream=AudioSystem.getAudioInputStream(new File(url));

之后还要添加延迟click.start(); i.e Thread.Sleep(4000);

或者,如果您想确保它播放整个音频样本,您可以使用一个简单的片段,例如

import javax.sound.sampled.*;
import java.io.File;

public class Main  implements LineListener {
private boolean done = false;
public  void update(LineEvent event) {
    if(event.getType() == LineEvent.Type.STOP || event.getType() == LineEvent.Type.CLOSE) {
      done = true;
    }
}

public void waitonfinish() throws InterruptedException {
   while(!done) {
       Thread.sleep(1000);
   } 
}
public static  void playSound(final String url) {

    try {
      Clip clip = AudioSystem.getClip();
      AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(url));
      Main control = new Main();
      clip.addLineListener(control);
      clip.open(inputStream);
      clip.start();
      control.waitonfinish();

    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
  }

public static void main(String[] args) {
    // TODO code application logic here
    playSound("C:\\warning_test.wav");   
 }
}

`

于 2012-04-13T05:00:13.053 回答
0

您完全复制了代码而没有注意到原始代码,它指向

path/to/sounds

既然你给了它完整的路径,你应该用 url 替换它:

 AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream(url));

编辑:我在这里试过,也得到了空值。我将其更改为从文件创建 audioInput:

import java.io.File;

import javax.sound.sampled.*;

public class Main {

    public static synchronized void playSound(final File file) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    Clip clip = AudioSystem.getClip();
                    AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
                    clip.open(inputStream);
                    clip.start();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.err.println(e.getMessage());
                }
            }
        }).start();
    }

    public static void main(String[] args) {
        // TODO code application logic here
        File file = new File("C:\\warning_test.wav");
        playSound(file);
    }
于 2012-04-13T05:23:12.170 回答