Android C ++ Recording Audio PCM file

General PCM recording file, simply modify tinycap code, where the package tinycap wav removed, can be directly recorded PCM files (header + principle WAV = PCM data). You can also use Android C ++ class of AudioSource record. The code is very simple, as follows:

#include <binder/ProcessState.h>
#include <media/mediarecorder.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/AudioSource.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MetaData.h>
#include <system/audio.h>

#define LOG_TAG "recordAudio"

using namespace android;

int main() {
    static const int32_t kSampleRate = 16000;
    static const int32_t kNumChannels = 1;
    static const int32_t kRecordTime = 20;  //20 seconds
    android::ProcessState::self()->startThreadPool();

    FILE *fp = fopen("/data/misc/media/audio.pcm","wb");
    if (fp == NULL) {
        return 0;
    }

    sp<AudioSource> audioSource = new AudioSource(
            AUDIO_SOURCE_CAMCORDER,  /*main mic: AUDIO_SOURCE_DEFAULT*/
            kSampleRate /* sampleRate */,
            kNumChannels /* channelCount */);

    status_t err = audioSource->initCheck();
    if (err != OK) {
        ALOGE("audio source is not initialized, initCheck failed");
        return 0;
    } else {
        sp<MetaData> params = new MetaData;
        params->setInt64(kKeyTime, 0ll);
        err = audioSource->start(params.get());
    }

    int64_t start_record = systemTime();
    while (1) {
        MediaBuffer *mbuf;
        err = audioSource->read(&mbuf);
        if (err != OK) {
            ALOGE("read audio data failed, err: %d", err);
            break;
        } else {
            int64_t timeUs;
            mbuf->meta_data()->findInt64(kKeyTime, &timeUs);
            ALOGD("audio frame timeUs: %lld", timeUs);
            fwrite((const uint8_t *)mbuf->data() + mbuf->range_offset(), 1, mbuf->range_length(), fp);
            mbuf->release();
            mbuf = NULL;
        }
        int64_t record_time = ((systemTime() - start_record) / 1000000000L);
        if (record_time > kRecordTime) {  //record 20 seconds
            ALOGE("record audio completed");
            break;
        }
    }

    audioSource->stop();
    audioSource.clear();
    fclose(fp);

    return 0;
}

The code can be placed in the framework \ av \ cmds \ stagefright \ directory to be compiled. AudioSource like to see framwork \ ... \ AudioSource.cpp file.

Published 30 original articles · won praise 24 · views 10000 +

Guess you like

Origin blog.csdn.net/q1075355798/article/details/104357938