我正在尝试使用 Java 从我的 Mac 上的麦克风录制音频。在 Google 上进行了一番搜索后,我在最底部找到了代码,但是当我在 Eclipse 中运行它时,每 3 或 4 次就有 1 次出现错误:
java(4946,0x1095f9000) malloc: *** error for object 0xd07400083: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
出现在控制台上。根据此处的另一篇文章,它与 Apple 为 OSX 提供的流氓库有关,但我不知道在 Java 中捕获音频的任何其他方式。有没有办法通过更改我正在使用的 Java 代码来防止这个错误,或者这超出了代码的范围?
这是我正在使用的代码:
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
public class AudioCapture extends Thread
{
private TargetDataLine dataLine;
private AudioFileFormat.Type audioFileType;
private AudioInputStream incomingStream;
private File generatedFile;
public AudioCapture(TargetDataLine line,
AudioFileFormat.Type requiredFileType,
File file){
dataLine = line;
incomingStream = new AudioInputStream(line);
audioFileType = requiredFileType;
generatedFile = file;}
public void startCapture(){
dataLine.start();
super.start();}
public void stopCapture(){
dataLine.stop();
dataLine.close();}
public void run(){
try{
AudioSystem.write(
incomingStream,
audioFileType,
generatedFile);}
catch (IOException e){
e.printStackTrace();}
}
public static void main(String[] args){
String strFilename = "captureFile.wav";
File outputFile = new File(strFilename);
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
22050.0F, 16, 2,
4, 22050.0F, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
audioFormat);
TargetDataLine targetDataLine = null;
try{
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);}
catch (LineUnavailableException e){
System.out.println("Unable to acquire an audio recording line");
e.printStackTrace();
System.exit(1);}
System.out.println("AudioFormat settings: " + audioFormat.toString());
AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
AudioCapture recorder = new AudioCapture(
targetDataLine,
targetType,
outputFile);
recorder.startCapture();
System.out.println("Starting the recording now...");
System.out.println("Please press to stop the recording.");
try{
System.in.read();}
catch (IOException e){
e.printStackTrace();}
recorder.stopCapture();
System.out.println("Capture stopped.");}
}