gpt4 book ai didi

Java AudioSystem 和 TargetDataLine

转载 作者:搜寻专家 更新时间:2023-11-01 01:59:05 25 4
gpt4 key购买 nike

我正在尝试从我的 PC 的线路输入中捕获音频,为此我正在使用 AudioSystem类(class)。静态 AudioSystem.write 方法有两种选择之一:写入文件或写入流。我可以让它很好地写入文件,但每当我尝试写入流时,我都会抛出 java.io.IOException(未指定流长度)。至于我的缓冲区,我使用的是 ByteArrayOutputStream。是否有另一种流我应该在其他地方使用或弄乱?

另外在相关主题中,可以通过调用 TargetDataLine 直接对 ( read ) 中的音频线进行采样.这是进行音频捕获还是使用 AudioSystem 的首选方式?

更新请求的源代码:

final private TargetDataLine line;
final private AudioFormat format;
final private AudioFileFormat.Type fileType;
final private AudioInputStream audioInputStream;
final private ByteArrayOutputStream bos;

// Constructor, etc.

public void run()
{
System.out.println("AudioWorker Started");
try
{
line.open(format);
line.start();

// This commented part is regarding the second part
// of my question
// byte[] buff = new byte[512];
// int bytes = line.read(buff, 0, buff.length);

AudioSystem.write(audioInputStream, fileType, bos);

}
catch ( Exception e )
{
e.printStackTrace();
}

System.out.println("AudioWorker Finished");
}


// Stack trace in console
AudioWorker Started
java.io.IOException: stream length not specified
at com.sun.media.sound.WaveFileWriter.write(Unknown Source)
at javax.sound.sampled.AudioSystem.write(Unknown Source)
at AudioWorker.run(AudioWorker.java:41)
AudioWorker Finished

最佳答案

来自 AudioSystem.write JavaDoc:

Writes a stream of bytes representing an audio file of the specified file type to the output stream provided. Some file types require that the length be written into the file header; such files cannot be written from start to finish unless the length is known in advance. An attempt to write a file of such a type will fail with an IOException if the length in the audio file type is AudioSystem.NOT_SPECIFIED.

关于Java AudioSystem 和 TargetDataLine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/598344/

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