2

我正在尝试从设备获取音频输入。在获取音频输入时,我尝试根据音频输入的强度将图标分配给标签。我使用以下代码:

public void run() {
        this.rec.startRecording();
        while (!this.done) {
            int nshorts = this.readBlock();
            if (nshorts <= 0)
                break;
        }
        //assignIcons();  
        this.rec.stop();
        this.rec.release();
    }

    int readBlock() {
        short[] buf = new short[this.block_size];

        int nshorts = this.rec.read(buf, 0, buf.length);
        if (nshorts > 0) {
            //Log.d(getClass().getName(), "Posting " + nshorts + " samples to queue");
            this.q.add(buf);
        }

        new Thread(new  Runnable() {
            public void run() {
                assignIcons(); 
            }
        }).start();
        return nshorts;
    }

    public void assignIcons(){
        //Add(Start)
        byte audioBuffer[]   = new  byte[this.minBufferSizeInBytes];//minBufferSizeInBytes = AudioRecord.getMinBufferSize( 8000,
                AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
        float totalAbsValue = 0.0f;
        short sample = 0;
        int numberOfReadBytes = 0; 
        float tempFloatBuffer[] = new float[3];
        int tempIndex = 0;
        //Add(End)
      //Add(Start)
        numberOfReadBytes = this.rec.read( audioBuffer, 0, this.minBufferSizeInBytes );
        for( int i=0; i<numberOfReadBytes; i+=2 ) {
            sample = (short)( (audioBuffer[i]) | audioBuffer[i + 1] << 8 );
            if(sample!=0) {
                totalAbsValue += Math.abs( sample ) / (numberOfReadBytes/2);
            }
        }
        tempFloatBuffer[tempIndex%3] = totalAbsValue;
        temp = 0.0f;
        for( int i=0; i<3; ++i )
            temp += tempFloatBuffer[i];
        mCamBase = CameraBaseActivity.getInstance();
        mCamBase.handler.postDelayed(mCamBase.AnimateRunnable, 10);

        //Add(End)

    }

我遇到的问题是,我不断得到以下日志,但没有得到想要的输出。我确信我显示的代码确实有问题,但不确定真正的问题是什么导致给出的日志连续输出。非常感谢解决此问题的任何帮助。

    01-03 09:24:40.930: DEBUG/co.nyk.android.audio.RecognizerTask(19152): signalling START
01-03 09:24:40.930: DEBUG/co.nyk.android.audio.RecognizerTask(19152): signalled START
01-03 09:24:40.930: DEBUG/co.nyk.android.audio.RecognizerTask(19152): gotSTART
01-03 09:24:40.930: DEBUG/co.nyk.android.audio.RecognizerTask(19152): START
01-03 09:24:41.836: WARN/AudioRecord(19152): obtainBuffer timed out (is the CPU pegged?) user=00001000, server=00001000
01-03 09:24:42.086: WARN/AudioRecord(19152): obtainBuffer timed out (is the CPU pegged?) user=00001800, server=00001800
01-03 09:24:43.321: WARN/AudioRecord(19152): obtainBuffer timed out (is the CPU pegged?) user=00004000, server=00004000
01-03 09:24:43.571: WARN/AudioRecord(19152): obtainBuffer timed out (is the CPU pegged?) user=00004800, server=00004800
01-03 09:24:43.836: WARN/AudioRecord(19152): obtainBuffer timed out (is the CPU pegged?) user=00005000, server=00005000
01-03 09:24:44.094: WARN/AudioRecord(19152): obtainBuffer timed out (is the CPU pegged?) user=00005800, server=00005800
01-03 09:24:44.391: WARN/AudioRecord(19152): obtainBuffer timed out (is the CPU pegged?) user=00006000, server=00006000
01-03 09:24:44.649: WARN/AudioRecord(19152): obtainBuffer timed out (is the CPU pegged?) user=00006800, server=00006800
01-03 09:24:45.118: WARN/AudioRecord(19152): obtainBuffer timed out (is the CPU pegged?) user=00007800, server=00007800
01-03 09:24:45.368: WARN/AudioRecord(19152): obtainBuffer timed out (is the CPU pegged?) user=00008000, server=00008000
4

0 回答 0