无法从 MIC 读取数据

发布于 2024-12-02 05:12:39 字数 983 浏览 1 评论 0原文

我正在尝试从 MIC 读取数据并对其进行处理并将其存储在文件中。但我没有从 MIC 获得任何数据,缓冲区全为零。

int MIN_BUF = AudioRecord.getMinBufferSize(8000,
        AudioFormat.CHANNEL_CONFIGURATION_MONO,
        AudioFormat.ENCODING_PCM_16BIT);
AudioRecord recorder = new AudioRecord(
        MediaRecorder.AudioSource.MIC, 8000,
        AudioFormat.CHANNEL_CONFIGURATION_MONO,
        AudioFormat.ENCODING_PCM_16BIT, MIN_BUF);
byte[] pcm_in = new byte[320];

recorder.startRecording();

while(record)
{
    int bytes_read = recorder.read(pcm_in, 0, pcm_in.length);

    switch(bytes_read)
    {
    case AudioRecord.ERROR_INVALID_OPERATION:
    case AudioRecord.ERROR_BAD_VALUE:                   
        Log.i("Microphone", "Error in reading the data");
        break;
    default:
        print(pcm_in);
        break;
    }
}

recorder.stop();
recorder.release();

但是在 print(pcm) 中,当我逐字节打印时,我得到的全是零。 stackoverflow 上有一些帖子有类似的问题,但我的问题没有得到解决。

请帮我解决这个问题。

谢谢&问候,
S苏曼185

I am trying to read the data from the MIC and process it and store it in a file. But i am not getting any data from the MIC, the buffer is all zeroes.

int MIN_BUF = AudioRecord.getMinBufferSize(8000,
        AudioFormat.CHANNEL_CONFIGURATION_MONO,
        AudioFormat.ENCODING_PCM_16BIT);
AudioRecord recorder = new AudioRecord(
        MediaRecorder.AudioSource.MIC, 8000,
        AudioFormat.CHANNEL_CONFIGURATION_MONO,
        AudioFormat.ENCODING_PCM_16BIT, MIN_BUF);
byte[] pcm_in = new byte[320];

recorder.startRecording();

while(record)
{
    int bytes_read = recorder.read(pcm_in, 0, pcm_in.length);

    switch(bytes_read)
    {
    case AudioRecord.ERROR_INVALID_OPERATION:
    case AudioRecord.ERROR_BAD_VALUE:                   
        Log.i("Microphone", "Error in reading the data");
        break;
    default:
        print(pcm_in);
        break;
    }
}

recorder.stop();
recorder.release();

But in the print(pcm), when i printed byte by byte i am getting all zeroes. Some posts are there in stackoverflow with similar issues, but my issue didn't got fixed with that.

Please help me in fixing this.

Thanks & Regards,
SSuman185

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不一样的天空 2024-12-09 05:12:39

print(pcm_in) 将显示实际数据。您需要循环获取 pcm_in 数据到 pcm,直到停止录制。

这意味着您的变量记录是一个布尔值。你会用另一种方法使它成为假。所以直到你设置为 false recorder.read(pcm_in, 0, pcm_in.length) 操作将从你的麦克风获取数据并将其放入 pcm_in (所以你需要确保 pcm_in 的大小等于 pcm)。 bytes_read 将是此操作中读取的字节大小。因此您可以在循环中将 pcm_in 字节复制到 pcm,从而可以读取整个 pcm_in 数据。

例如:

bytes_read = recorder.read(pcm_in, 0, pcm_in.length);

for(int i=0; i<bytes_read ;i++){
        pcm[i] = pcm_in[i];
 }

但这是一个奇怪的用法。我认为你的 pcm 应该和你需要加载的文件一样大。并确保您将 pcm_in 添加到其中,而不是覆盖。我想这就是你想要的。

print(pcm_in) will show you the actual data. you need to get the pcm_in data to pcm in a loop till you stop recording.

Meaning that your variable record is a boolean right. and you will make it false in another method. so till you make it false recorder.read(pcm_in, 0, pcm_in.length) operation will get the data from your mic and put it into the pcm_in(so you need to be sure that the size of pcm_in is equal to pcm). the bytes_read will be the size of the bytes read in this operation. so you can copy the pcm_in bytes to pcm in a loop that can read whole pcm_in data.

for example:

bytes_read = recorder.read(pcm_in, 0, pcm_in.length);

for(int i=0; i<bytes_read ;i++){
        pcm[i] = pcm_in[i];
 }

But this is a weird usage. I think your pcm should be as large as the file you need to load in it. and make sure you are addin the pcm_in to it , not overriding. I think this is what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文