如何在不使用AudioSessionSetProperty的情况下将音频路由到扬声器? [英] How Do I Route Audio to Speaker without using AudioSessionSetProperty?

查看:132
本文介绍了如何在不使用AudioSessionSetProperty的情况下将音频路由到扬声器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 AudioSessionSetProperty 可能会变成已弃用,我正在尝试查找如何将音频路由到的代码示例发言人使用其他方式。

As AudioSessionSetProperty may become deprecated, I'm trying to find an code example of how to route audio to the speaker using other means.

之前我做了以下事情:

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

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

尝试获得相同的结果,但没有通话 AudioSessionSetProperty

Trying to get same result but without call to AudioSessionSetProperty.

推荐答案

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

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

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

Since iOS 6 kAudioSessionProperty_OverrideAudioRoute is represented in AVAudioSession by the method

- (BOOL)overrideOutputAudioPort:error:

可用值 AVAudioSessionPortOverrideNone AVAudioSessionPortOverrideSpeaker

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

Here is an example audio session configured entirely via 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

自iOS 7.0起,音频会话服务C API现已完全弃用,转而支持AVAudioSession。

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

更新2

- (BOOL)overrideOutputAudioPort:error:  

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

is a method, not a property, and it sets an underlying write-only UInt32 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.

这篇关于如何在不使用AudioSessionSetProperty的情况下将音频路由到扬声器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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