gpt4 book ai didi

java - Java-播放录制的音频-LineUnavailableException

转载 作者:行者123 更新时间:2023-12-03 02:11:43 25 4
gpt4 key购买 nike

我正在用Java将一些音频记录到ByteArrayOutputStream中,但是当我尝试播放该字节数组时,出现LineUnavailableException。这是我播放音频的代码:

public void playTone(ByteArrayOutputStream 
try {
AudioFormat format = new AudioFormat(44100, 8, 2, true, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
byte audio[] = note.toByteArray();
InputStream input = new ByteArrayInputStream(audio);
TargetDataLine line1 = (TargetDataLine) AudioSystem.getLine(info);
...
} catch (LineUnavailableException e1) {
e1.printStackTrace();
}
}

这是我得到的异常(exception):
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 8 bit, stereo, 2 bytes/frame,  not supported.

现在,我已经在 this中阅读了一个问题,即AudioFormat的参数可能是错误的,但是在该问题中,用户使用24(位)作为第二个参数,其中仅支持8和16。

有人知道我在做什么错吗?

编辑:

从以下答案中,我更改了以下内容,因为我没有文件,但是有ByteArrayOutputStream:
AudioFormat format = new AudioFormat(44100, 16, 2, true, true);
byte audio[] = note.toByteArray();
InputStream input = new ByteArrayInputStream(audio);
audioStream = new AudioInputStream(input, format, audio.length / format.getFrameSize());
// instead of
// audioStream = AudioSystem.getAudioInputStream(soundFile);

最佳答案

您可以从中清除您的概念。我认为这对您有帮助。

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class MakeSound {

private final int BUFFER_SIZE = 128000;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;

/**
* @param filename the name of the file that is going to be played
*/
public void playSound(String filename){

String strFilename = filename;

try {
soundFile = new File(strFilename);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}

try {
audioStream = AudioSystem.getAudioInputStream(soundFile);
} 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;
byte[] abData = new byte[BUFFER_SIZE];
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);
}
}

sourceLine.drain();
sourceLine.close();
}
}

关于java - Java-播放录制的音频-LineUnavailableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23393483/

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