iOS錄音的幾個函數調用

來源:互聯網
上載者:User

int i, bufferByteSize;

UInt32 size;

CFURLRef url;

readarri=0;

try {

mFileName = CFStringCreateCopy(kCFAllocatorDefault, inRecordFile);

// specify the recording format

SetupAudioFormat(kAudioFormatLinearPCM);

// create the queue

XThrowIfError(AudioQueueNewInput(

  &mRecordFormat,

  MyInputBufferHandler,

  this /* userData */,

  NULL /* run loop */, NULL /*
run loop mode */,

  0 /* flags */, &mQueue), "AudioQueueNewInput
failed");

// get the record format back from the queue's audio converter --

// the file may require a more specific stream description than was necessary to create the encoder.

mRecordPacket = 0;

size = sizeof(mRecordFormat);

XThrowIfError(AudioQueueGetProperty(mQueue, kAudioQueueProperty_StreamDescription,

&mRecordFormat, &size), "couldn't get queue's format");

NSString *recordFile = [NSTemporaryDirectory() stringByAppendingPathComponent:
(NSString*)inRecordFile];

url = CFURLCreateWithString(kCFAllocatorDefault, (CFStringRef)recordFile, NULL);

// create the audio file

XThrowIfError(AudioFileCreateWithURL(url, kAudioFileCAFType,
&mRecordFormat,kAudioFileFlags_EraseFile,

  &mRecordFile), "AudioFileCreateWithURL failed");

CFRelease(url);

// copy the cookie first to give the file object as much info as we can about the data going in

// not necessary for pcm, but required for some compressed audio

CopyEncoderCookieToFile();

// allocate and enqueue buffers

bufferByteSize = ComputeRecordBufferSize(&mRecordFormat, kBufferDurationSeconds);//
enough bytes for half a second

for (i = 0; i < kNumberRecordBuffers; ++i) {

XThrowIfError(AudioQueueAllocateBuffer(mQueue, bufferByteSize, &mBuffers[i]),

  "AudioQueueAllocateBuffer failed");

XThrowIfError(AudioQueueEnqueueBuffer(mQueue, mBuffers[i], 0, NULL),

  "AudioQueueEnqueueBuffer failed");

}

// start the queue

mIsRunning = true;

XThrowIfError(AudioQueueStart(mQueue, NULL), "AudioQueueStart
failed");

}

catch (CAXException &e) {

char buf[256];

fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));

}

catch (...) {

fprintf(stderr, "An unknown
error occurred\n");

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.