【问题标题】:AudioRecord - writing PCM fileAudioRecord - 写入 PCM 文件
【发布时间】:2012-11-27 12:01:08
【问题描述】:

我正在尝试使用 AudioRecord 类录制一些声音,然后将其写入输出 .pcm 文件。我希望我的程序继续录制,直到按下停止按钮。不幸的是,无论我录制多长时间,输出文件大小始终为 3528 字节,持续时间约为 20 毫秒。同样根据 Toolsoft Audio Tools,该文件的属性是:44100Hz,16 位,立体声,即使我使用的是采样频率完全不同的单声道。

Thread recordingThread;
boolean isRecording = false;


int audioSource = AudioSource.MIC;
int sampleRateInHz = 44100;
int channelConfig = AudioFormat.CHANNEL_IN_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);

byte Data[] = new byte[bufferSizeInBytes];

AudioRecord audioRecorder = new AudioRecord(audioSource,
                                            sampleRateInHz, 
                                            channelConfig, 
                                            audioFormat, 
                                            bufferSizeInBytes);
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);   
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}    

public void startRecording(View arg0) {
    audioRecorder.startRecording();
    isRecording = true;
    recordingThread = new Thread(new Runnable() {
        public void run() {
            String filepath = Environment.getExternalStorageDirectory().getPath();
            FileOutputStream os = null;
            try {
                os = new FileOutputStream(filepath+"/record.pcm");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            while(isRecording) {
                audioRecorder.read(Data, 0, Data.length);
                try {
                    os.write(Data, 0, bufferSizeInBytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    recordingThread.start();
}

public void stopRecording(View arg0) {
    if (null != audioRecorder) {
        isRecording = false;
        audioRecorder.stop();
        audioRecorder.release();
        audioRecorder = null;
        recordingThread = null;
    }
}

请问您有什么问题吗?我希望答案不会是“一切”:)

【问题讨论】:

标签: android audiorecord


【解决方案1】:
try {
                os.write(Data, 0, bufferSizeInBytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

这就是问题所在。您在一次写入后关闭 FileOutputStream (os.close())。 将其移出 while 循环:

while(isRecording) {
            audioRecorder.read(Data, 0, Data.length);
            try {
                os.write(Data, 0, bufferSizeInBytes);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
      try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

【讨论】:

    【解决方案2】:

    Change your sample rate to 8000,因为在 emulator 中你是cant test with 44100 sample rate.

    使用AudioRecord源如图在模拟器中播放

    private static final int RECORDER_SAMPLERATE = 8000;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
    
    AudioRecord audio_record = new AudioRecord(MediaRecorder.AudioSource.MIC,
                RECORDER_SAMPLERATE, RECORDER_CHANNELS,
                RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
    
    
    int BufferElements2Play = 1024; // want to play 2048 (2K) since 2 bytes we use only 1024
    int BytesPerElement = 2; // 2 bytes in 16bit format
    

    If it din't get worked, then go to this blog. It will work fine for recording and storing the data in .pcm file.

    【讨论】:

    • 我用的是手机,不是模拟器。
    • 在我按下启动录制的按钮后,使用了该博客中的整个代码和应用程​​序崩溃。
    • 将采样率更改为 44100 立体声编码 - 16 位,查看。它不应该崩溃,你能得到一个错误日志文件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多