10

I'm very new to Objective-C, and am trying to update some code that's about 3 years old to work with iOS 7. There are two or two instances of AudioSessionSetProperty and AudioSessionInitialize appearing in the code:

1:

- (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 works on iOS7 in the simulator but not a device running iOS7, and I suspect that these deprecations are the cause. I've been reading through the Docs and related questions on this website, and it appears that I need to use AVAudioSession instead. I've been trying to update the code for a long time now, and I'm unsure of how to properly switch over to AVAudioSession. Does anyone know how these two methods above need to look?

Side note: I've managed to hunt down an article that outlines the transition: https://github.com/software-mariodiana/AudioBufferPlayer/wiki/Replacing-C-functions-deprecated-in-iOS-7 But I can't seem to apply this to the code above.

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

Alternatively, if someone could point me to a sample demo app that can detect frequencies on iOS devices, that would be awesome.

Henry F
  • 4,960
  • 11
  • 55
  • 98

2 Answers2

15

As you have observed, pretty much all of the old Core Audio AudioSession functions have been deprecated in favour of AVAudioSession.

The AVAudioSession is a singleton object which will get initialised when you first call it:

[AVAudioSession sharedInstance]

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

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

As regards setting the hardware sample rate using AVAudioSession, please refer to my answer here:
How can I obtain the native (hardware-supported) audio sampling rates in order to avoid internal sample rate conversion?

For other compares & contrasts between Core Audio audioSession and AVFoundation's AVAudioSession here are some of my other answers around the same topic:

How Do I Route Audio to Speaker without using AudioSessionSetProperty?

use rear microphone of iphone 5

Play audio through upper (phone call) speaker

How to control hardware mic input gain/level on iPhone?

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125
7

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

Mario
  • 2,397
  • 2
  • 24
  • 41
  • this eliminated 4 deprecation warnings on 'AudioSessionInitialize', 'AudioSessionSetProperty' and 'AudioSessionSetActive' warning but how to deal a new warning _setDelegate is deprecated in iOS 6_ ? – Greg Jun 02 '16 at 21:39
  • oops, I should have added AVFoundation.framework. See [http://stackoverflow.com/questions/18807157/how-do-i-route-audio-to-speaker-without-using-audiosessionsetproperty/37604793#37604793] – Greg Jun 03 '16 at 02:09
  • is there any chance I could get you look at this http://stackoverflow.com/questions/37692276/could-nan-be-causing-the-occasional-crash-in-this-core-audio-ios-app – Greg Jun 08 '16 at 05:52
  • I will take a look, but please don't get your hopes up. I posted the tutorial that I wrote after figuring out how to get AVAudioSession working. At the time, I was was coding in Objective-C and interested in doing audio work. My programming career took me elsewhere, alas, and I am right now super rusty with any iOS stuff. I will vote your question up though, in the meantime. – Mario Jun 08 '16 at 17:54
  • 1
    not to worry. Thanks to your tutorial, I am already well on the way to being top of my problem, which as it turns out, was nothing to do with your upgrade tutorial. In fact, it will probably lead to a ringing endorsement of it. – Greg Jun 09 '16 at 06:50
  • 1
    I managed to solve my problem. It was possible because of your GitHub. Thanks. I also replaced - (BOOL)setUpAudioSession with foundry's - (void)configureAVAudioSession. See [link] (http://stackoverflow.com/questions/37692276/could-nan-be-causing-the-occasional-crash-in-this-core-audio-ios-app) – Greg Jun 12 '16 at 11:27
  • I remember that the sound on my actual phone was very low, something that I thought was strange. It looks like you got to the bottom of that. Thanks! – Mario Jun 13 '16 at 20:20
  • 1
    Your github article was very useful in helping me figure out how to resolve my different AudioSession deprecations. – DonnaLea Sep 29 '17 at 22:58