在Mac上捕获音频时,未分配被释放的指针?

时间:2012-04-07 22:57:21

标签: java javasound

我正在尝试使用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.");}
}

1 个答案:

答案 0 :(得分:0)

这是一个稍微雄心勃勃的行动方案,但是:

你可以完全按照它说的做并调试它(这会有帮助吗?我不确定)。如果安装CDT包,则可以使用gdb调试编译。