Java ogg文件的第一次播放有效,第二次播放LineUnavailableException
我正在尝试用Java循环ogg音频文件。我在用电话 我成功地播放了一次文件。当我尝试再次播放该文件或播放另一个文件时,我会收到Java ogg文件的第一次播放有效,第二次播放LineUnavailableException,java,javasound,oggvorbis,Java,Javasound,Oggvorbis,我正在尝试用Java循环ogg音频文件。我在用电话 我成功地播放了一次文件。当我尝试再次播放该文件或播放另一个文件时,我会收到 LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported. 我不知道我做错了什么 方法如下 public static void testLine(File file) {
LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.
我不知道我做错了什么
方法如下
public static void testLine(File file) {
try (AudioInputStream in = AudioSystem.getAudioInputStream(file)) {
AudioFormat inFormat = in.getFormat();
AudioFormat outFormat = new AudioFormat(PCM_SIGNED, inFormat.getSampleRate(),
16, inFormat.getChannels(), inFormat.getChannels() * 2, inFormat.getSampleRate(), false);
Info info = new Info(SourceDataLine.class, outFormat);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
if (line != null) {
line.open(outFormat);
FloatControl volume = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue((float) ambiance.audio.Track.DEFAULT_VOLUME);
// stream
line.start();
byte[] buffer = new byte[65536]; // is this magical? // yes: the highest number which can be represented by an unsigned 16-bit binary number
AudioInputStream stream = AudioSystem.getAudioInputStream(outFormat,in);
for (int n = 0; n != -1; n = stream.read(buffer, 0, buffer.length)) {
line.write(buffer, 0, n);
}
line.drain();
line.stop();
in.close();
retVal = true;
}
} catch (UnsupportedAudioFileException|LineUnavailableException|IOException e) {
JOptionPane.showMessageDialog(null, e.getMessage(),
e.getClass().toString(), JOptionPane.ERROR_MESSAGE);
}
}
完成后,需要在
行调用close()
。完成后,需要在行调用close()
。可能重复@Wuaner,如问题所述,它会成功播放一次。在另一个问题中,它涉及MP3,而不是OGG。一个答案建议做我已经做过的事情,另一个使用JLayer来播放MP3文件。如问题中所述,@Wuaner可能重复,它成功地播放了一次。在另一个问题中,它涉及MP3,而不是OGG。一个答案建议做我已经做过的事情,另一个使用JLayer来播放MP3文件。哦,天哪,就这些了:(我到家后再试,谢谢。哦,天哪,就这些了。)(我到家后再试,谢谢。