Java Sound Api:将录音保存到磁盘后无法播放 Wav 文件
我正在从麦克风录制声音并将其作为 .wav 文件保存到磁盘。
我无法使用媒体播放器播放 wav 文件?这是正常的吗?
AudioFormat format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
44100.0F, 16, 2, 4, 44100.0F, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); // format is an AudioFormat object
// Obtain and open the line.
try {
targetLine = (TargetDataLine) AudioSystem.getLine(info);
targetLine.open(format);
targetLine.start();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int numBytesRead;
byte[] data = new byte[targetLine.getBufferSize() / 5];
targetLine.start();
while (recording) {
numBytesRead = targetLine.read(data, 0, data.length);
out.write(data, 0, numBytesRead);
System.out.println(Arrays.toString(data));
}
File fileOut = new File("C:\\audio\\audio6.wav");
AudioInputStream ais = new AudioInputStream(targetLine);
if (AudioSystem. isFileTypeSupported(AudioFileFormat.Type.WAVE,ais)) {
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, fileOut);
System.out.println("writen");
}
targetLine.stop();
ais.close();
I am making a sound recording from the mic and saving it as a .wav file to disk.
I can't play the wav file with Media Player ? Is this normal ?
AudioFormat format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
44100.0F, 16, 2, 4, 44100.0F, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); // format is an AudioFormat object
// Obtain and open the line.
try {
targetLine = (TargetDataLine) AudioSystem.getLine(info);
targetLine.open(format);
targetLine.start();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int numBytesRead;
byte[] data = new byte[targetLine.getBufferSize() / 5];
targetLine.start();
while (recording) {
numBytesRead = targetLine.read(data, 0, data.length);
out.write(data, 0, numBytesRead);
System.out.println(Arrays.toString(data));
}
File fileOut = new File("C:\\audio\\audio6.wav");
AudioInputStream ais = new AudioInputStream(targetLine);
if (AudioSystem. isFileTypeSupported(AudioFileFormat.Type.WAVE,ais)) {
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, fileOut);
System.out.println("writen");
}
targetLine.stop();
ais.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果文件工作正常,您应该能够在媒体播放器中播放该文件。
我建议获取免费音频编辑器 Audacity (http://audacity.sourceforge.net/) 的副本并查看数据。
首先检查文件中是否有一些数据,然后检查字节序、位深度和采样率是否正确(切换并查看它们中是否有任何一个产生明显的噪音)。
如果这不起作用,请尝试从文件而不是麦克风中读取一些样本,然后检查它们到达另一端时是否相同。
如果您还没有找到,这是 Java 音频的绝佳资源
http://www.jsresources.org/
You should be able to play the file in media player if it's working correctly.
I would recommend getting a copy of the free audio editor Audacity (http://audacity.sourceforge.net/) and looking at the data.
Firstly check that there is some data in the file and then check that you have the endian, bit depth and sample rate correct (Switch around and see if any of them make a sensible noise).
If that doesn't work try reading in some samples from a file rather than the Mic and then checking they are the same when they come our the other end.
If you haven't found it already this is a great resource for Java Audio
http://www.jsresources.org/
您将记录的数据加载到 ByteArrayOutputStream 中,然后不对它执行任何操作(正如 Jon Skeet 所指出的)。
当“录音”布尔值为 false 时,那么您将 TargetDataLine 附加到 AudioInputStream 吗?但如果记录错误,那么我假设没有数据通过 TDL。数据是通过 read() 方法从 TDL 获取的,并且位于 while 循环内。
我认为可以通过 ByteArrayInputStream 制作 AIS,该 ByteArrayInputStream 可以由用于从 TDL 收集数据的 ByteArrayOutputStream 制作。
因此,在记录布尔值变为 false 后,尝试以这种方式设置您的 AIS:
You are loading the recorded data into the ByteArrayOutputStream out, and then never doing anything with it (as pointed out by Jon Skeet).
When the "recording" boolean is false, THEN you attach the TargetDataLine to the AudioInputStream? But if recording is false, then I assume there is no data coming through the TDL. Data is obtained from a TDL via the read() method, and it is within the while loop.
I think it is possible to make an AIS via the ByteArrayInputStream which can be made from the ByteArrayOutputStream that you used to collect the data from the TDL.
Thus, after the recording boolean goes false, try making your AIS this way: