gpt4 book ai didi

ios - 检测耳机是否插入 - iOS 5

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:11 25 4
gpt4 key购买 nike

我知道这是一个已经问过的问题,我发现了可能的重复问题:

Detecting if headphones are plugged into iPhone

headphone plug-in plug-out event when audio route doesn't change - iOS

Detect if headphones (not microphone) are plugged in to an iOS device

...以及有关 WWW 的更多信息。但我已经尝试了所有给出的解决方案,每次遇到问题时,可能是因为它们是旧线程并且指的是 iOS 4。我如何在 iOS 5.0 上检测到它?谢谢

最佳答案

如果您对仅限 iOS 6 的解决方案没问题,Apple 添加了几个新的 AVAudioSession 属性,让您只需几行即可检测音频路由(无需使用 C)。

使用此方法检查耳机(或调整它以检查其他输出 - “扬声器”、“耳机”等):

- (BOOL)isHeadsetPluggedIn
{
// Get array of current audio outputs (there should only be one)
NSArray *outputs = [[AVAudioSession sharedInstance] currentRoute].outputs;

NSString *portName = [[outputs objectAtIndex:0] portName];

if ([portName isEqualToString:@"Headphones"]) {
return YES;
}

return NO;
}

如果您想被动响应音频路由更改,可以使用新的 NSNotification AVAudioSessionRouteChangeNotification 来实现。不幸的是,这个通知并没有告诉你新路线是什么,只是告诉你它切换的前一条路线。但是,您可以调用上述方法的一些变体来获取当前路线。

关于ios - 检测耳机是否插入 - iOS 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16187757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com