From 269689fb5112e7d471a10ccaade5376b8a200de8 Mon Sep 17 00:00:00 2001 From: Steve Hannah Date: Tue, 26 Jun 2018 05:33:01 -0700 Subject: [PATCH] Fixed issue with creating multiple AudioComponents when calling AudioController.prepareWithSampleRate: more than once. --- .../Speech/AudioController.m | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/speech/Objective-C/Speech-gRPC-Streaming/Speech/AudioController.m b/speech/Objective-C/Speech-gRPC-Streaming/Speech/AudioController.m index 0d45aaa5..db25c0df 100644 --- a/speech/Objective-C/Speech-gRPC-Streaming/Speech/AudioController.m +++ b/speech/Objective-C/Speech-gRPC-Streaming/Speech/AudioController.m @@ -20,6 +20,7 @@ @interface AudioController () { AudioComponentInstance remoteIOUnit; + BOOL audioComponentInitialized; } @end @@ -126,6 +127,7 @@ - (OSStatus) prepareWithSampleRate:(double) specifiedSampleRate { AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *error; + BOOL ok = [session setCategory:AVAudioSessionCategoryRecord error:&error]; NSLog(@"set category %d", ok); @@ -143,20 +145,24 @@ - (OSStatus) prepareWithSampleRate:(double) specifiedSampleRate { double sampleRate = session.sampleRate; NSLog (@"hardware sample rate = %f, using specified rate = %f", sampleRate, specifiedSampleRate); sampleRate = specifiedSampleRate; - - // Describe the RemoteIO unit - AudioComponentDescription audioComponentDescription; - audioComponentDescription.componentType = kAudioUnitType_Output; - audioComponentDescription.componentSubType = kAudioUnitSubType_RemoteIO; - audioComponentDescription.componentManufacturer = kAudioUnitManufacturer_Apple; - audioComponentDescription.componentFlags = 0; - audioComponentDescription.componentFlagsMask = 0; - - // Get the RemoteIO unit - AudioComponent remoteIOComponent = AudioComponentFindNext(NULL,&audioComponentDescription); - status = AudioComponentInstanceNew(remoteIOComponent,&(self->remoteIOUnit)); - if (CheckError(status, "Couldn't get RemoteIO unit instance")) { - return status; + if (!audioComponentInitialized) { + + audioComponentInitialized = YES; + // Describe the RemoteIO unit + AudioComponentDescription audioComponentDescription; + audioComponentDescription.componentType = kAudioUnitType_Output; + audioComponentDescription.componentSubType = kAudioUnitSubType_RemoteIO; + audioComponentDescription.componentManufacturer = kAudioUnitManufacturer_Apple; + audioComponentDescription.componentFlags = 0; + audioComponentDescription.componentFlagsMask = 0; + + // Get the RemoteIO unit + AudioComponent remoteIOComponent = AudioComponentFindNext(NULL,&audioComponentDescription); + status = AudioComponentInstanceNew(remoteIOComponent,&(self->remoteIOUnit)); + if (CheckError(status, "Couldn't get RemoteIO unit instance")) { + return status; + } + } UInt32 oneFlag = 1;