如何在Java中循环播放*.wav?
如何在到达结尾时循环播放*.wav?
我的代码如下所示:
public class SoundPlayer implements Runnable{
public SoundPlayer(String filename){
is=Main.class.getResourceAsStream("sounds/"+filename);
}
@Override
public void run() {
// TODO Auto-generated method stub
playSound();
}
public void playSound(){
try {
audioStream = AudioSystem.getAudioInputStream(is);
} catch (Exception e){
e.printStackTrace();
System.exit(1);
}
audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
} catch (LineUnavailableException e) {
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
sourceLine.start();
int nBytesRead = 0;
int bufferSize = audioFormat.getFrameSize() *
Math.round(audioFormat.getSampleRate() / 10);
byte[] abData = new byte[bufferSize];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
@SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
else {
audioStream.reset(); //I add this code but didn't effect
}
}
sourceLine.drain();
sourceLine.close();
}
private final int BUFFER_SIZE = 128000;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
private InputStream is;
请帮助我。
How to play *.wav loop when reach end?
My code look like this:
public class SoundPlayer implements Runnable{
public SoundPlayer(String filename){
is=Main.class.getResourceAsStream("sounds/"+filename);
}
@Override
public void run() {
// TODO Auto-generated method stub
playSound();
}
public void playSound(){
try {
audioStream = AudioSystem.getAudioInputStream(is);
} catch (Exception e){
e.printStackTrace();
System.exit(1);
}
audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
} catch (LineUnavailableException e) {
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
sourceLine.start();
int nBytesRead = 0;
int bufferSize = audioFormat.getFrameSize() *
Math.round(audioFormat.getSampleRate() / 10);
byte[] abData = new byte[bufferSize];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
@SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
else {
audioStream.reset(); //I add this code but didn't effect
}
}
sourceLine.drain();
sourceLine.close();
}
private final int BUFFER_SIZE = 128000;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
private InputStream is;
Help me please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我创建这个类是为了在单独的线程中播放
.wav
声音。如果您愿意,请随意使用和适应:干杯!
I created this class to play
.wav
sound in a separate thread. Feel free to use and adapt if you want:Cheers!
如果是简短的声音示例,请使用
剪辑
。使用loop(int)
方法设置重复次数。请参阅此处一个例子。
If it is a short sound sample, use a
Clip
. Use theloop(int)
method to set how many times to repeat.See here for an example.