IOS: obsolescence of AudioSessionInitialize and AudioSessionSetProperty

I am very new to Objective-C, and I'm trying to update code that has been running iOS 7 for about 3 years. Two or two instances of AudioSessionSetProperty and AudioSessionInitialize appear in the code:

one

 - (void)applicationDidFinishLaunching:(UIApplication *)application { AudioSessionInitialize(NULL,NULL,NULL,NULL); [[SCListener sharedListener] listen]; timer = [NSTimer scheduledTimerWithTimeInterval: 0.5 target: self selector: @selector(tick:) userInfo:nil repeats: YES]; // Override point for customization after app launch [window addSubview:viewController.view]; [window makeKeyAndVisible]; } 

AND 2:

 - (id)init { if ([super init] == nil){ return nil; } AudioSessionInitialize(NULL,NULL,NULL,NULL); Float64 rate=kSAMPLERATE; UInt32 size = sizeof(rate); AudioSessionSetProperty (kAudioSessionProperty_PreferredHardwareSampleRate, size, &rate); return self; } 

For some reason, this code runs on iOS7 in a simulator, but not on a device running iOS7, and I suspect that these costs are the reason. I read Docs and related issues on this website and it seems to me that I need to use AVAudioSession . I tried to update the code for a long time, and I'm not sure how to switch to AVAudioSession . Does anyone know what these two methods should look like above?

Side note: I was able to track down an article that describes the transition: https://github.com/software-mariodiana/AudioBufferPlayer/wiki/Replacing-C-functions-deprecated-in-iOS-7 But I can not apply this to the above above code.

The code I'm trying to update is a small frequency detection application from git: https://github.com/jkells/sc_listener

Alternatively, if someone could point me to an example of a demo application that can detect frequencies on iOS devices, that would be awesome.

+8
objective-c ios7 avaudiosession
source share
2 answers

As you may have noticed, almost all of the old Core Audio AudioSession features are deprecated in favor of AVAudioSession.

AVAudioSession is a singleton object that will be initialized the first time it is called:

 [AVAudioSession sharedInstance] 

There is no separate initialize method. But you want to activate an audio session:

 BOOL activated = [[AVAudioSession sharedInstance] setActive:YES error:&error]; 

Regarding setting the sampling rate of equipment using AVAudioSession , refer to my answer here:
How to get the internal (hardware supported) audio sampling rate to avoid internal conversion of the sampling rate?

For other comparisons and contrasts between Core Audio audioSession and AVFoundation AVAudioSession, here are some of my other answers on the same topic:

How to disable an audio disc without using AudioSessionSetProperty?

use iphone 5 rear microphone

Play audio through the top (phone call) speaker

How to control microphone gain / input level on iPhone?

+14
source share

I wrote a short tutorial that discusses how to update new AVAudioSession objects. I posted it on GitHub: "Replacing C features deprecated in iOS 7."

+7
source share

All Articles