1

嗨,我正在构建一个应用程序,它将使用来自 MIC 的传入音频并将其与存储的声音文件进行比较。目前,我正试图掌握 AudioRecord 函数中的数据在保存到字节数组时的样子。我的问题是返回的值都是零。我不知道我是否可能没有正确使用/设置 AudioRecord 功能。这是我的代码:

import android.app.Activity;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SnoreAlarmActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */

Button start, stop;
TextView display;
Boolean rec = false;
AudioRecord snore;
byte[] arrayOfByte = new byte[16 * 1024 / 8];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    start = (Button) findViewById(R.id.bStart);
    stop = (Button) findViewById(R.id.bStop);
    display = (TextView) findViewById(R.id.tAnswer);

    start.setOnClickListener(this);
    stop.setOnClickListener(this);

    int i = AudioRecord.getMinBufferSize(44100, 16, 2);
    snore = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, 16, 2, i);// From
                                                                            // MIC,Sample
                                                                            // Rate
                                                                            // of
                                                                            // 44100,
                                                                            // Channel_IN_MONO,16Bit
                                                                            // Encoding,buffer
                                                                            // size
                                                                            // i
    new Record().start();
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {

    case R.id.bStart:
        rec = true;
        break;

    case R.id.bStop:
        rec = false;
        snore.stop();
        snore.release();

        for (int i = 0; i < 100; i++) {
            int x = (int) arrayOfByte[i];
            Log.w("Tag", "" + x);

        }
        break;
    }
}

class Record extends Thread {
    Record() {

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (rec) {
            snore.startRecording();
            snore.read(arrayOfByte, 0, (16 * 1024 / 8));
        }
    }

   }
}

所以我的问题是,为什么我得零?*另外我知道由于 16Bit 编码,我应该将数据存储在一个简短的位置,稍后会这样做,只是试图了解目前从 AudioRecord 函数返回的值。

4

2 回答 2

1

通过 MIC 录音让我头疼 :)

希望这会有所帮助,因为我整天都在玩它。以下是 MIC 工作必须满足的步骤

  1. Maniefst.xml 必须包含:

    < 使用权限 android:name="android.permission.RECORD_AUDIO" />

  2. AudioRecord 缓冲区应该使用 AudioRecord.getMinBufferSize 设置(你做对了)

  3. 如果 MIC 未释放,您将无法再次绑定 MIC!重启手机将重置 MIC。

这是我如何做到的简短示例。

private android.media.AudioRecord aRecorder = null;
private Boolean breakLoop = false;
private byte[] buffer;
public Boolean StartRecording()
    int freq = 22050;
    try
    {
        prepareWaveFile(MyRandomAccessFile); //open file and writes WAV header
        int bufferSize = android.media.AudioRecord.getMinBufferSize(freq, android.media.AudioFormat.CHANNEL_IN_MONO, android.media.AudioFormat.ENCODING_PCM_16BIT);
        buffer = new byte[bufferSize];
        if(bufferSize == AudioRecord.ERROR_BAD_VALUE){
                Log.e(LOG_TAG, "Min buff size error");
                return false;
        }

        aRecorder = new android.media.AudioRecord(MediaRecorder.AudioSource.MIC, freq, android.media.AudioFormat.CHANNEL_IN_MONO, android.media.AudioFormat.ENCODING_PCM_16BIT, bufferSize);

        aRecorder.startRecording();
        breakLoop = false;
        int TotalSize=0; //koliko je snimljeno podataka
        while (TotalSize < freq * 2 * 1 * 30) { //SampleRate * BytesPerSample * NumberOfChannels * seconds  
            int bufferReadResult = aRecorder.read(buffer, 0, bufferSize);
            TotalSize+=bufferReadResult; // filesize failsafe
            if (breakLoop) break; //if other thread stops recording
            if (bufferReadResult>0) MyRandomAccessFile.write(buffer, 0, bufferReadResult); //write into some file ...
        }

        aRecorder.stop();
        aRecorder.release(); //this is mandatory !
        updateWaveFile(MyRandomAccessFile); //updates wave header
        MyRandomAccessFile.close();
        return true;
    } catch (Exception e)
    {
        Log.e(LOG_TAG, "StartRecording: LOOP " + e);

        try
        {
            aRecorder.stop();
        } catch(Exception ex) {}

        try
        {
            aRecorder.release();
        } catch(Exception ex) {}

        try
        {
            randomAccessWriter.close();
        } catch(Exception ex) {}
        return false;
    }    
于 2013-02-20T11:06:22.733 回答
0

你在模拟器上吗?听说麦克风不能用。

于 2012-07-21T21:57:47.210 回答