gpt4 book ai didi

java - 打开 TargetDataLine 时未引发异常

转载 作者:行者123 更新时间:2023-12-02 07:47:30 24 4
gpt4 key购买 nike

根据Javadocs,当我使用javax.sound.sampledTargetDataLine的以下方法时:

public void open(AudioFormat format,int bufferSize)

其中说:

Invoking this method with a requested buffer size that does not meet this requirement may result in an IllegalArgumentException.`

所以当我实现以下Java代码时:

AudioFormat format = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class,format);

// open the TargetDataLine for capture.
try {
TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format, line.getBufferSize()+3000);
}
catch (LineUnavailableException ex)
{
ex.printStackTrace();
}

但是当我运行上面的代码时它不会抛出任何异常。现在根据文档:

public int getBufferSize()
Obtains the maximum number of bytes of data that will fit in the data line's internal buffer. For a source data line, this is the size of the buffer to which data can be written. For a target data line, it is the size of the buffer from which data can be read. Note that the units used are bytes, but will always correspond to an integral number of sample frames of audio data.

这表示将返回最大尺寸,并且我添加了 3000

line.open(format, line.getBufferSize()+3000); 

如上所示,为什么它没有抛出任何异常?

最佳答案

您遗漏了 JavaDoc 中有关 open 的一些重要描述。更完整地说,它说:

void open(AudioFormat format,
int bufferSize)
throws LineUnavailableException

Opens the line with the specified format and requested buffer size, causing the line to acquire any required system resources and become operational. The buffer size is specified in bytes, but must represent an integral number of sample frames. Invoking this method with a requested buffer size that does not meet this requirement may result in an IllegalArgumentException. The actual buffer size for the open line may differ from the requested buffer size. The value actually set may be queried by subsequently calling DataLine.getBufferSize()

因此,您指定的缓冲区大小可能不是内部缓冲区的大小,甚至可能超过它。

但是,它必须“表示整数个样本帧”,否则将抛出 IllegalArgumentException。如果您请求的内存多于内部分配的内存,那么您将收到LineUnavailableException。这并不意味着请求比当前内部缓冲区更大的大小将导致 LineUnavailableException

至少这是我对文档的阅读。

关于java - 打开 TargetDataLine 时未引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10633425/

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