Thursday, June 6, 2013

AVAudioSession setPreferredSampleRate example in Objective C (iOS).


AVAudioSession setPreferredSampleRate

Sets the preferred sample rate for input and output.

- (BOOL)setPreferredSampleRate:(double)sampleRate error:(NSError **)outError

Parameters of [AVAudioSession setPreferredSampleRate]
sampleRate
The hardware sample rate you want to use. The available range for hardware sample rate is device dependent. It typically ranges from 8,000 through 48,000 hertz.
outError
On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in nil.

Return Value
YES if the preferred sample rate was accepted and set or NO if it was not.

Discussion of [AVAudioSession setPreferredSampleRate]
The audio hardware input and output sample rates are always equal to each other when input and output are both running. For more information, see “Querying and Using Audio Hardware Characteristics” in Audio Session Programming Guide.

AVAudioSession setPreferredSampleRate example.
- (void) setUp {
    AVAudioSession *sess = [AVAudioSession sharedInstance];
    NSError *error = nil;
    rate = 44100.0;
    [sess setPreferredSampleRate:rate error:&error];
    [sess setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    [sess setActive:YES error:&error];
    rate = [sess sampleRate];
    if (error) {
        NSLog(@"%@", error);
    }

    NSLog(@"Init...");
    [self createUnitDesc];
    [self getComponent];
    [self getAudioUnit];
    [self enableIORec];
    [self enableIOPb];
    [self createFormat];
    [self applyFormat];
    OSStatus err = AudioUnitInitialize(unit);
    if (noErr != err) {
        [self showStatus:err];
    }
}

End of AVAudioSession setPreferredSampleRate example article.