gpt4 book ai didi

java :clip line not working

转载 作者:行者123 更新时间:2023-12-01 04:52:11 25 4
gpt4 key购买 nike

这是我正在使用的代码。我使用了 Clip 类来播放剪辑。程序已编译,没有任何错误并且运行正常,但我听不到声音。

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;


public class ClipTest {

public static void main(String[] args) throws Exception {


File soundFile = new File("./1.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);


DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);


clip.addLineListener(new LineListener() {
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
event.getLine().close();
System.exit(0);
}
}
});


clip.start();
}
}

最佳答案

我只是尝试你的代码。我认为您的错误是您的文件为空或未正确加载

我只是替换

File soundFile = new File("./1.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);

InputStream inRequest = this.getClass().getResourceAsStream("1.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(inRequest);

这是新类

public class ClipTest {

public void run() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
InputStream inRequest = this.getClass().getResourceAsStream("batR.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(inRequest);

DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);

clip.addLineListener(new LineListener() {

public void update(LineEvent event) {
if(event.getType() == LineEvent.Type.STOP) {
event.getLine().close();
System.exit(0);
}
}
});

clip.start();

}

public static void main(String[] args) throws Exception {
ClipTest clipTest = new ClipTest();
clipTest.run();

}
}

关于java :clip line not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14786244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com