我怎么能写的SourceDataLine的内容保存到一个文件? [英] How can I write the contents of a SourceDataLine to a file?

查看:369
本文介绍了我怎么能写的SourceDataLine的内容保存到一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改播放音频数据,以将数据写入到一个文件,而不是一个应用程序。因为它是目前实现的,一个字节数组是动态填充,并在每次填充时间这个缓冲器的内容被写入到的SourceDataLine。基本上,我想要写在指定的格式,缓冲到文件中。

我已经通过这个官方教程阅读并在此来到$ C $ç剪断音频数据写入文件:

 文件FILEOUT =新的文件(someNewPathName);
为AudioFileFormat.Type的fileType = fileFormat.getType();
如果(AudioSystem.isFileTypeSupported(的fileType,
    的AudioInputStream)){
  AudioSystem.write(的AudioInputStream,文件类型,FILEOUT);
}

我从我可以构建使用TargetDataLine的的AudioInputStream API文档看,然而,在我来说,我有一个的SourceDataLine。我不知道如何从我的字节数组数据到TargetDataLine的,因为它实现了read()方法,而不是写的()。在该的AudioInputStream的其他用途和其他文件把它当作从的文件中读取的的一种方式,所以我有点糊涂了其与AudioSystem.write使用()。

那么,怎样才能我从SourceDataLine的获取数据,或直接从缓存,成TargetDataLine的或的AudioInputStream以便它可以写入到文件中?


解决方案

  1. 使用字节[] 建立 ByteArrayInputStream的

  2. 提供BAIS到<一个href=\"http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html#getAudioInputStream%28java.io.InputStream%29\"相对=nofollow> AudioSystem.getAudioInputStream(InputStream的)

  3. 使用AIS在 AudioSystem.write(..)

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?

解决方案

  1. Use the byte[] to establish a ByteArrayInputStream
  2. Provide the BAIS to AudioSystem.getAudioInputStream(InputStream)
  3. Use the AIS in AudioSystem.write(..)

这篇关于我怎么能写的SourceDataLine的内容保存到一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆