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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18807157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:11:44  来源:igfitidea点击:

How Do I Route Audio to Speaker without using AudioSessionSetProperty?

iosobjective-cavaudiosession

提问by Jim Hankins

As AudioSessionSetPropertymay become deprecated, I'm trying to find an code example of how to route audio to the speakerusing other means.

作为AudioSessionSetProperty有可能成为deprecated,我试图找到如何将音频路由到一个代码示例speaker使用其他手段。

Previously I did the following:

以前我做了以下事情:

-(void)setSpeakerEnabled
{
    debugLog(@"%s",__FUNCTION__);
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

    AudioSessionSetProperty (
                         kAudioSessionProperty_OverrideAudioRoute,
                         sizeof (audioRouteOverride),
                         &audioRouteOverride
                         );
}

Trying to get same result but without callto AudioSessionSetProperty.

试图获得相同的结果,但without callAudioSessionSetProperty.

回答by foundry

On each release of iOS, more of the audioSession properties are migrated to AVFoundation, so you should use those in preference whenever available.

在 iOS 的每个版本中,更多的 audioSession 属性都迁移到了 AVFoundation,因此您应该在可用时优先使用这些属性。

Since iOS 6 kAudioSessionProperty_OverrideAudioRouteis represented in AVAudioSession by the method

由于 iOS 6kAudioSessionProperty_OverrideAudioRoute在 AVAudioSession 中由方法表示

- (BOOL)overrideOutputAudioPort:error:

Available values are AVAudioSessionPortOverrideNoneand AVAudioSessionPortOverrideSpeaker

可用值是AVAudioSessionPortOverrideNoneAVAudioSessionPortOverrideSpeaker

Here is an example audio session configured entirely via AVFoundation:

这是一个完全通过 AVFoundation 配置的音频会话示例:

 - (void)configureAVAudioSession
{
   // Get your app's audioSession singleton object
    AVAudioSession *session = [AVAudioSession sharedInstance];

    // Error handling
    BOOL success;
    NSError *error;

    // set the audioSession category. 
    // Needs to be Record or PlayAndRecord to use audioRouteOverride:  

    success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
                             error:&error];

   if (!success) {
       NSLog(@"AVAudioSession error setting category:%@",error);
   }

    // Set the audioSession override
    success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
                                          error:&error];
    if (!success) {
        NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);
    }

    // Activate the audio session
    success = [session setActive:YES error:&error];
    if (!success) {
        NSLog(@"AVAudioSession error activating: %@",error);
    }
    else {
         NSLog(@"AudioSession active");
    }

}

UPDATE

更新

Since iOS 7.0, the Audio Session Services C API is now fully deprecated in favour of AVAudioSession.

自 iOS 7.0 起,Audio Session Services C API 现在已完全弃用,取而代之的是 AVAudioSession。

UPDATE 2

更新 2

- (BOOL)overrideOutputAudioPort:error:  

is a method, not a property, and it sets an underlying write-onlyUInt32 value. You can't get the current value, and you should treat the method as setting a temporary state. If the audio route changes or is interrupted, the property resets to its default (AVAudioSessionPortOverrideNone). You can get interruption notifications via AVAudioSessionDelegate.

是一个方法,而不是一个属性,它设置了一个底层只写UInt32 值。您无法获取当前值,您应该将该方法视为设置临时状态。如果音频路由更改或中断,该属性将重置为其默认值 ( AVAudioSessionPortOverrideNone)。您可以通过 获取中断通知AVAudioSessionDelegate

回答by Yunus Nedim Mehel

NSError *error;
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker 
                                                   error:&error];    
if(error)
{ 
    NSLog(@"Error: AudioSession cannot use speakers");
}

回答by Mirac Bektas

let audioSession = AVAudioSession.sharedInstance()

    do {
        try audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: .spokenAudio, options: .defaultToSpeaker)
        try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
    } catch {
        print("error.")
    }  

// paste this code in your viewLoad region

// 将此代码粘贴到您的 viewLoad 区域中

回答by Greg

Foundry's solution together with this blogby Mario Diana has also allowed me to upgrade audio session set up code deprecated in iOS 7. My old code was based on AudioBufferPlayerby Matthijs Hollemans. Remember to add the AVFoundation.framework.

Foundry 的解决方案以及Mario Diana 的这篇博客也让我能够升级 iOS 7 中弃用的音频会话设置代码。我的旧代码基于 Matthijs Hollemans 的AudioBufferPlayer。记得添加 AVFoundation.framework。