ios – 我想每秒调用20次installTapOnBus:bufferSize:format:block:

前端之家收集整理的这篇文章主要介绍了ios – 我想每秒调用20次installTapOnBus:bufferSize:format:block:前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从麦克风实时输入波形显示.
我已经使用installTapOnBus:bufferSize:format:block:来实现,这个函数在一秒钟内被调用三次.
我想将此函数设置为每秒调用20次.
我在哪里可以设置?
AVAudioSession *audioSession = [AVAudioSession sharedInstance];

NSError* error = nil;
if (audioSession.isInputAvailable) [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
if(error){
    return;
}

[audioSession setActive:YES error:&error];
if(error){
    retur;
}

self.engine = [[[AVAudioEngine alloc] init] autorelease];

AVAudioMixerNode* mixer = [self.engine mainMixerNode];
AVAudioInputNode* input = [self.engine inputNode];
[self.engine connect:input to:mixer format:[input inputFormatForBus:0]];

// tap ... 1 call in 16537Frames
// It does not change even if you change the bufferSize
[input installTapOnBus:0 bufferSize:4096 format:[input inputFormatForBus:0] block:^(AVAudioPCMBuffer* buffer,AVAudioTime* when) {

    for (UInt32 i = 0; i < buffer.audioBufferList->mNumberBuffers; i++) {
        Float32 *data = buffer.audioBufferList->mBuffers[i].mData;
        UInt32 frames = buffer.audioBufferList->mBuffers[i].mDataByteSize / sizeof(Float32);

        // create waveform
        ...
    }
}];

[self.engine startAndReturnError:&error];
if (error) {
    return;
}

解决方法

他们说,Apple支持回复否:( 2014年9月)

Yes,currently internally we have a fixed tap buffer size (0.375s),
and the client specified buffer size for the tap is not taking effect.

但有人调整缓冲区大小,并获得40毫秒
https://devforums.apple.com/thread/249510?tstart=0

在ObjC中无法检查它:(

UPD它有效!只是单行:

[input installTapOnBus:0 bufferSize:1024 format:[mixer outputFormatForBus:0] block:^(AVAudioPCMBuffer *buffer,AVAudioTime *when) {
    buffer.frameLength = 1024; //here
原文链接:https://www.f2er.com/iOS/332568.html

猜你在找的iOS相关文章