AVAudioSession操纵声音输出 [英] AVAudioSession manipulate sound output

查看:1162
本文介绍了AVAudioSession操纵声音输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AVSoundSession 来配置声音,而使用 AVAudioPlayer 来播放不同的声音。
我搜索了很多,找不到任何东西。如何操作输出源?

I'm using AVSoundSession to configure sound, and AVAudioPlayer to play different sounds. I searched a lot and couldn't find anything. How can I manipulate output sources?

我需要一个方法在我的 SoundManager 中我可以在手机扬声器之间切换输出和扬声器。

I need a method in my SoundManager where I could switch output between phone speaker and loudspeaker.

success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
                                     error:&error];

使用此功能,我可以将声音传送到扬声器,但没有办法将其移动到手机扬声器。任何人都可以帮我吗?

Using this I can route sound to loudspeaker, but there is no method to move it to phone speaker. Can anybody help me with it?

推荐答案

所以,我找到了使用声音输出进行操作的解决方案。

So, I found solution for manipulating with sound output.

您可以使用初始化声音设置AVAudioSession

这样的事情:

session = [AVAudioSession sharedInstance];

BOOL success;
NSError* error;

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

if (!success)  NSLog(@"AVAudioSession error setting category:%@",error);
success = [session setMode:AVAudioSessionModeVoiceChat error:&error];

if (!success)  NSLog(@"AVAudioSession error setting mode:%@",error);

success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];

[session setPreferredOutputNumberOfChannels:0 error:nil];
if (!success)  NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);

success = [session setActive:YES error:&error];
if (!success) NSLog(@"AVAudioSession error activating: %@",error);
else NSLog(@"audioSession active");

使用

[session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];

您设置不覆盖输出端口。你的应用程序使用默认扬声器播放。据我所知,模式 AVAudioSessionModeVoiceChat 使用手机扬声器。这是我的SIP来电应用程序所需的直接内容。

You set don't override output port. And your app playing with default speaker. As I understand for mode AVAudioSessionModeVoiceChat used phone speaker. It's directly what I need for my SIP caller app.

然后你可以用

[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];

我这样做:

- (void) loudSpeakerOn:(BOOL)isLoudSpeaker{
    [session setActive:NO error:nil];

    BOOL success;
    NSError* error;

    success = [session overrideOutputAudioPort:isLoudSpeaker?AVAudioSessionPortOverrideSpeaker:AVAudioSessionPortOverrideNone error:&error];

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

    [session setActive:YES error:nil];
}

这篇关于AVAudioSession操纵声音输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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