尝试播放音频(.wav)文件时无声音 [英] No Sound When Trying to Play Audio (.wav) Files
本文介绍了尝试播放音频(.wav)文件时无声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非常简单的类,可以使用以下代码播放声音文件:
I have a very simple class that can play a sound file with the following code:
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sound{
private Clip sound;
public Sound(String location){
try{
sound = AudioSystem.getClip();
File file = new File(location);
sound.open(AudioSystem.getAudioInputStream(file));
}
catch(IOException | LineUnavailableException | UnsupportedAudioFileException error){
System.out.println(error);
}
}
public void play(){
sound.start();
}
}
但是,当我创建此类的实例并在其上调用play函数时,没有任何声音.声音开始和结束时会听到a啪声,但实际文件却听不到.而且我也没有任何错误.
However, when I create an instance of this class and call the play function on it I don't get any sound. I hear a pop when the sound starts and when it ends but not the actual file. Also I don't get any errors of any sort.
我在做什么错了?
推荐答案
尝试类似的方法:
File soundFile = new File( "something.wav" );
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream( soundFile );
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();//This plays the audio
加载音频流后,您可能必须使用AudioSystem.getClip()
.
You might have to use AudioSystem.getClip()
after loading the audio stream.
这篇关于尝试播放音频(.wav)文件时无声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文