机器人,AudioRecord.read() - GT; bufferoverflow,如何处理缓冲区? [英] android, AudioRecord.read() --> bufferoverflow, how to handle the buffer?

查看:1490
本文介绍了机器人,AudioRecord.read() - GT; bufferoverflow,如何处理缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一所大学的项目我的教授。要我写一个Android应用程序,将是我的第一个。我有一些Java的经验,但我是新来的Andr​​oid程序,所以请善待我。

首先,我创建活动,我只有两个按钮,一个用于启动的AsyncTask,一个用于阻止它,我的意思是我刚才设置的布尔isRecording为假,其他的一切在AsyncTask的,它连接处理源$ C ​​$ C。

事情正在运行相当不错,但一段时间后,我可以找到LogCat中一些bufferoverflow消息之后,它有一个未捕获的异常崩溃。我想通了,为什么它的崩溃,并且未捕获的异常不应该是这个问题的目的。

 十一月3日至七号:34:02.474:信息/缓冲器247:(558):40
11月3号至7号:34:02.484:WARN / AudioFlinger(33):RecordThread:缓冲区溢出
11月3号至7号:34:02.484:信息/ MutantAudioRecorder:doInBackground()(558):isRecoding
11月3号至7号:34:02.484:信息/ MutantAudioRecorder:doInBackground()(558):isRecoding
11月3号至7号:34:02.494:WARN / AudioFlinger(33):RecordThread:缓冲区溢出
11月3号至7号:34:02.494:信息/缓冲器248:(558):-50
 

  1. 我写出来的缓冲区,你可以看到,但不知何故,我想我在正确配置AudioRecord犯了一个错误,有谁能够告诉我为什么得到bufferoverflow?

  2. 和下一个问题会是这样,我怎么能处理缓冲区?我的意思是,我有它里面的价值,并希望他们在屏幕上的图形频谱显示。有没有人有这经验,我可以给一个提示?我该如何去...

在此先感谢您的帮助。

来源$ C ​​$的AsyncTask的的C:

 包nomihodai.audio;

进口android.media.AudioFormat;
进口android.media.AudioRecord;
进口android.os.AsyncTask;
进口android.util.Log;



公共类MutantAudioRecorder扩展的AsyncTask<虚空,虚空,虚空> {

私人布尔isRecording = FALSE;
公共AudioRecord audioRecord = NULL;
公众诠释mSamplesRead;
公众诠释buffersizebytes;
公众诠释buflen;
公众诠释channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
公众诠释audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
公共静态短[]缓冲区;
公共静态最终诠释SAMPLESPERSEC = 8000;


@覆盖
保护无效doInBackground(虚空...... PARAMS){

    而(isRecording){

        audioRecord.startRecording();
        mSamplesRead = audioRecord.read(缓冲液,0,buffersizebytes);

        如果(!readerT.isAlive())
            readerT.start();

        Log.i(MutantAudioRecorder:doInBackground(),isRecoding);
    }

    readerT.stop();

    返回null;
}


螺纹READER™软件=新的Thread(){
    公共无效的run(){
        的for(int i = 0; I< 256;我++){
            Log.i(缓冲+ I +:,Short.toString(缓冲[I]));
        }
    }
};


@覆盖
公共无效onPostExecute(作废不用){
    Log.i(MutantAudioRecorder:onPostExecute(),尝试释放音频硬件);

    audioRecord.release();

    Log.i(MutantAudioRecorder:onPostExecute(),发布...);
}


公共无效setRecording(布尔REC){
    this.isRecording = REC;

    Log.i(MutantAudioRecorder:setRecording(),isRecoding设为+ REC);
}


@覆盖
在preExecute保护无效(){

    buffersizebytes = AudioRecord.getMinBufferSize(SAMPLESPERSEC,channelConfiguration,audioEncoding);
    缓冲区=新的短[buffersizebytes]
    buflen = buffersizebytes / 2;

    Log.i(MutantAudioRecorder:上preExecute(),buffersizebytes:+ buffersizebytes
                                                +,缓冲液:+ buffer.length
                                                +,buflen:+ buflen);

    audioRecord =新AudioRecord(android.media.MediaRecorder.AudioSource.MIC,
            SAMPLESPERSEC,
            channelConfiguration,
            audioEncoding,
            buffersizebytes);

    如果(audioRecord!= NULL)
        Log.i(MutantAudioRecorder:在preExecute(),audiorecord对象创建);
    其他
        Log.i(MutantAudioRecorder:在preExecute(),audiorecord没有创建);
}
 

}

解决方案

这也可能是一些现场分析过程正在录制的音频字节?

自的缓冲大小的记录是有限的,一旦你的分析处理比记录速度慢,在缓冲器中的数据将被卡住了,但是记录字节总是来从而缓冲区溢出。

尝试在录制字节上记录使用线程和其他进程,有一个开放源码的样品$ C $下这种方法:的 HTTP://musicg.google$c$c.com/files/musicg_android_demo.zip

for a university project my prof. wants me to write an android application, would be my first one. I have some Java experience but I am new to Android programming, so please be gentle with me.

First I create an Activity where I have only two buttons, one for starting an AsyncTask and one for stopping it, I mean I just set the boolean "isRecording" to false, everything else is handled in the AsyncTask, which is attached as source code.

The thing is running quite okay, but after a while I can find some bufferoverflow messages in the LogCat and after that it crashes with an uncaught exception. I figured out why it's crashing, and the uncaught exception shouldn't be the purpose of that question.

03-07 11:34:02.474: INFO/buffer 247:(558): 40
03-07 11:34:02.484: WARN/AudioFlinger(33): RecordThread: buffer overflow
03-07 11:34:02.484: INFO/MutantAudioRecorder:doInBackground()(558): isRecoding
03-07 11:34:02.484: INFO/MutantAudioRecorder:doInBackground()(558): isRecoding
03-07 11:34:02.494: WARN/AudioFlinger(33): RecordThread: buffer overflow
03-07 11:34:02.494: INFO/buffer 248:(558): -50

  1. I write out the buffer as you can see, but somehow I think I made a mistake in configuring the AudioRecord correctly, can anybody tell why I get the bufferoverflow?

  2. And the next question would be, how can I handle the buffer? I mean, I have the values inside it and want them to show in graphical spectrogram on the screen. Does anyone have experience with it and can me give a hint? How can I go on ...

Thanks in advance for your help.

Source code of the AsyncTask:

package nomihodai.audio;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.os.AsyncTask;
import android.util.Log;



public class MutantAudioRecorder extends AsyncTask<Void, Void, Void> {

private boolean isRecording = false;
public AudioRecord audioRecord = null;
public int mSamplesRead;
public int buffersizebytes;
public int buflen;
public int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
public static short[] buffer;
public static final int SAMPLESPERSEC = 8000;


@Override
protected Void doInBackground(Void... params) {

    while(isRecording) {

        audioRecord.startRecording();
        mSamplesRead = audioRecord.read(buffer, 0, buffersizebytes);

        if(!readerT.isAlive())
            readerT.start();

        Log.i("MutantAudioRecorder:doInBackground()", "isRecoding");
    }

    readerT.stop();

    return null;
}


Thread readerT = new Thread() {
    public void run() {
        for(int i = 0; i < 256; i++){ 
            Log.i("buffer " + i + ": ", Short.toString(buffer[i]));
        }
    }
};


@Override
public void onPostExecute(Void unused) {
    Log.i("MutantAudioRecorder:onPostExecute()", "try to release the audio hardware");

    audioRecord.release();

    Log.i("MutantAudioRecorder:onPostExecute()", "released...");
}


public void setRecording(boolean rec) {
    this.isRecording = rec;

    Log.i("MutantAudioRecorder:setRecording()", "isRecoding set to " + rec);
}


@Override
protected void onPreExecute() {

    buffersizebytes = AudioRecord.getMinBufferSize(SAMPLESPERSEC, channelConfiguration, audioEncoding);
    buffer = new short[buffersizebytes];
    buflen = buffersizebytes/2;

    Log.i("MutantAudioRecorder:onPreExecute()", "buffersizebytes: " + buffersizebytes
                                                + ", buffer: " + buffer.length
                                                + ", buflen: " + buflen);

    audioRecord = new AudioRecord(android.media.MediaRecorder.AudioSource.MIC,
            SAMPLESPERSEC,
            channelConfiguration,
            audioEncoding,
            buffersizebytes);

    if(audioRecord != null)
        Log.i("MutantAudioRecorder:onPreExecute()", "audiorecord object created");
    else
        Log.i("MutantAudioRecorder:onPreExecute()", "audiorecord NOT created");
}

}

解决方案

It's probably some live analyzing process working on the recorded audio bytes?

Since the buffer size for recording is limited, once your "analyzing process" is slower than the rate of recording, the data in the buffer will be stuck, but the recording bytes are always coming thus buffer overflows.

Try use threads on recording and the other process on the recorded bytes, there's a open source sample code for this approach: http://musicg.googlecode.com/files/musicg_android_demo.zip

这篇关于机器人,AudioRecord.read() - GT; bufferoverflow,如何处理缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆