iOS:AudioSessionInitialize和AudioSessionSetProperty的弃用 [英] iOS: Deprecation of AudioSessionInitialize and AudioSessionSetProperty

查看:1236
本文介绍了iOS:AudioSessionInitialize和AudioSessionSetProperty的弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective-C的新手,我正在尝试更新一些大约3年的代码来使用iOS 7.有两个或两个实例 AudioSessionSetProperty AudioSessionInitialize 出现在代码中:

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];
}

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;
}

出于某种原因,此代码适用于模拟器中的iOS7,但不适用于运行的设备iOS7,我怀疑这些弃用是原因。我一直在阅读本网站上的文档和相关问题,看来我需要使用 AVAudioSession 。我一直在尝试更新代码很久了,我不确定如何正确切换到 AVAudioSession 。有谁知道上面这两种方法需要怎么看?

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?

附注:我已经成功找到了一篇概述过渡的文章:
https://github.com/software-mariodiana/AudioBufferPlayer/wiki/替换-c-functions-in-iOS-7
但我似乎无法将其应用于上述代码。

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.

我正在尝试更新的代码是来自git的小型频率检测应用程序:
https://github.com/jkells/sc_listener

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

或者,如果有人可以指向我可以检测iOS设备上的频率的示例演示应用程序,那就太棒了。

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

推荐答案

正如您所观察到的,几乎所有旧的Core Audio AudioSession函数都已被弃用,转而支持AVAudioSession。

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

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];

关于使用设置硬件采样率AVAudioSession ,请参考我的答案:

如何获得原生(硬件支持)音频采样率以避免内部采样率转换?

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?

其他比较& Core Audio audioSession和AVFoundation的AVAudioSession之间的对比是我围绕同一主题的一些其他答案:

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

如何在不使用AudioSessionSetProperty的情况下将音频路由到扬声器?

使用iphone 5后置麦克风

通过上层(电话)扬声器播放音频

如何在iPhone上控制硬件麦克风输入增益/电平?

这篇关于iOS:AudioSessionInitialize和AudioSessionSetProperty的弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆