如何将SourceDataLine的内容写入文件?(How can I write the contents of a SourceDataLine to a file?)

编程入门 行业动态 更新时间:2024-10-24 01:48:30
如何将SourceDataLine的内容写入文件?(How can I write the contents of a SourceDataLine to a file?)

我正在修改播放音频数据的应用程序,以便将数据写入文件。 当前实现时,动态填充字节数组,每次填充时,此缓冲区的内容都写入SourceDataLine。 我基本上想要将缓冲区写入指定格式的文件中。

我已经阅读了这个官方教程,并且发现这段代码已被剪切,用于将音频数据写入文件:

File fileOut = new File(someNewPathName); AudioFileFormat.Type fileType = fileFormat.getType(); if (AudioSystem.isFileTypeSupported(fileType, audioInputStream)) { AudioSystem.write(audioInputStream, fileType, fileOut); }

我从API文档中看到我可以使用TargetDataLine构造一个AudioInputStream,但在我的例子中我有一个SourceDataLine。 我不知道如何将我的字节数组中的数据导入TargetDataLine,因为它实现了read()方法而不是write()。 AudioInputStream的其他用途以及其他文档将其视为文件中读取的一种方式,因此我对它与AudioSystem.write()的使用感到有些困惑。

那么,如何将数据从SourceDataLine或直接从缓冲区获取到TargetDataLine或AudioInputStream中,以便将其写入文件?

I am modifying an application that plays audio data to write the data to a file instead. As it is currently implemented, a byte array is filled dynamically, and the contents of this buffer are written to a SourceDataLine each time it is filled. I basically want to write that buffer out to a file in a specified format.

I have read through this official tutorial and came across this code snipped for writing audio data to a file:

File fileOut = new File(someNewPathName); AudioFileFormat.Type fileType = fileFormat.getType(); if (AudioSystem.isFileTypeSupported(fileType, audioInputStream)) { AudioSystem.write(audioInputStream, fileType, fileOut); }

I see from the API documentation that I can construct an AudioInputStream using a TargetDataLine, however in my case I have a SourceDataLine. I don't know how to get the data from my byte array into the TargetDataLine since it implements the read() method instead of write(). Other uses of the AudioInputStream in that and other documentation treat it as a way of reading from a file, so I'm a little confused by its use with AudioSystem.write().

So, how can I get the data from a SourceDataLine, or from the buffer directly, into a TargetDataLine or AudioInputStream so that it can be written out to a file?

最满意答案

使用byte[]建立ByteArrayInputStream 将BAIS提供给AudioSystem.getAudioInputStream(InputStream) 在AudioSystem.write(..)使用AIS Use the byte[] to establish a ByteArrayInputStream Provide the BAIS to AudioSystem.getAudioInputStream(InputStream) Use the AIS in AudioSystem.write(..)

更多推荐

本文发布于:2023-08-07 01:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458547.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   文件   内容   SourceDataLine   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!