Android AudioRecord questions?

前端 未结 4 1063
广开言路
广开言路 2021-01-03 04:54

I have been messing around with the AudioRecord feature of the Android API and found some strange behaviors with it.

Background info: My phone is a HTC Incredible I

4条回答
  •  醉话见心
    2021-01-03 05:38

    I might be able to help a tiny bit. However, I am in the same situation as you so I can only tell you about my experiences.

    I believe you can get your device's preferred sampleRate like so:

    int sampleRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
    

    The 16Bit encoding mode is the only one that works with AudioRecord. I am unsure why you are getting 8Bit like output (right?). Maybe someone knows.

    I am also unsure how you pull the amplitude from the retrieved bytes, did you do this?

    Finally, I believe you need to use the periodic listener functions, like so:

    int sampleRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
    int channelMode = AudioFormat.CHANNEL_IN_MONO;
    int encodingMode = AudioFormat.ENCODING_PCM_16BIT;
    int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelMode, encodingMode);
    
    AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelMode, encodingMode, bufferSize);
    
    recorder.setPositionNotificationPeriod(intervalFrames);
    recorder.setRecordPositionUpdateListener(recordListener);
    recorder.startRecording();
    
    private RecordListener recordListener = new RecordListener();
    private class RecordListener implements AudioRecord.OnRecordPositionUpdateListener {
        public void onMarkerReached(AudioRecord recorder) {
            Log.v("MicInfoService", "onMarkedReached CALL");
        }
    
        public void onPeriodicNotification(AudioRecord recorder) {
            Log.v("MicInfoService", "onPeriodicNotification CALL");
        }
    }
    

    There are two big questions here which I cannot answer and want to know the answer of myself as well:

    1. What should be set as intervalFrames?
    2. Why are the listener methods never called?

    I wish I could help more.

提交回复
热议问题