正在尝试使用javax.sound注册语音

正在尝试使用javax.sound注册语音,java,audio,Java,Audio,我愿意使用javax.sound API从麦克风注册一些音频,但生成的文件无法被我的音频播放器读取 我编写了一个测试方法,它启动一个线程进行注册,等待几秒钟,通知中断注册,再等待几秒钟,然后将录制的音频保存到磁盘 下面是代码(不包括异常管理) 这段代码由我的执行者运行并进行注册,实际上生成了一个非空文件(684字节3秒,988字节4秒),但它不会被我的播放器打开(例如VLC) 我应该在哪里寻找问题?除了这种方法,你会推荐其他方法吗?下一步是再现录制的音频。谢谢。看起来您只是将读取的原始PCM字节

我愿意使用javax.sound API从麦克风注册一些音频,但生成的文件无法被我的音频播放器读取

我编写了一个测试方法,它启动一个线程进行注册,等待几秒钟,通知中断注册,再等待几秒钟,然后将录制的音频保存到磁盘

下面是代码(不包括异常管理)

这段代码由我的执行者运行并进行注册,实际上生成了一个非空文件(684字节3秒,988字节4秒),但它不会被我的播放器打开(例如VLC)


我应该在哪里寻找问题?除了这种方法,你会推荐其他方法吗?下一步是再现录制的音频。谢谢。

看起来您只是将读取的原始PCM字节写入文件,大多数玩家都不知道如何处理这种格式


您需要使用类似于
AudioSystem.write
的方法以可识别的格式写入文件。

看起来您只是在将已读取的原始PCM字节写入文件,大多数播放器都不知道如何处理这种格式。您需要使用类似于
AudioSystem.write
的方法以可识别的格式编写文件。这可能是我的问题。将AudioFormat更改为新的AudioFormat(16000,8,2,true,true)并将文件保存为Wav:AudioSystem.write(audioInputStream,AudioFileFormat.Type.WAVE,audioFile)就足够了@greg-449请您将您的评论移动到回答位置,以便我可以接受?
public void record() {
        VoiceRecorder voiceRecorder = new VoiceRecorder();
        Future<ByteArrayOutputStream> result = executor.submit(voiceRecorder);
        Thread.sleep(3000);

        voiceRecorder.signalStopRecording();

        Thread.sleep(1000);

        ByteArrayOutputStream audio = result.get();

        FileOutputStream stream = new FileOutputStream("./" + filename + ".mp3");
        stream.write(audio.toByteArray());
        stream.close();
}
public ByteArrayOutputStream call() {
AudioFormat standardFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 128, 16, 1, 2, 128, false);
TargetDataLine microphone = null;
microphone = AudioSystem.getTargetDataLine(format);    
microphone.open(format);

int numBytesRead;
byte[] data = new byte[microphone.getBufferSize() / 5];

// Begin audio capture.
microphone.start();

ByteArrayOutputStream recordedAudioRawData = new ByteArrayOutputStream();

while (!stopped) {
    // Read the next chunk of data from the TargetDataLine.
    numBytesRead = microphone.read(data, 0, data.length);
    // Save this chunk of data.
    recordedAudioRawData.write(data, 0, numBytesRead);
}

return recordedAudioRawData;
}