音频路由不变时耳机插件插件事件 - iOS [英] headphone plug-in plug-out event when audio route doesn't change - iOS
问题描述
我在iPad上工作。
我想检测用户拔出耳机的时间。首先,我在属性上使用了一个监听器 kAudioSessionProperty_AudioRouteChange
。所以一切都很顺利,直到我决定添加一个按钮,以便在耳机仍然插入时切换到扬声器。所以我现在面临一个问题,也许有人会有想法来解决它。
I'm working on iPad.
I would like to detect when user plug-out headphone. First I used a listener on the property kAudioSessionProperty_AudioRouteChange
. So all was working well until I've decided to add a button to switch to speakers when headphone was still plugged. So I’m now facing a problem, maybe someone would have an idea to fix it.
这是一个场景:
- 我插上一个耳机 - >我的音频路由更改回调被称为
- 然后我将声音切换到扬声器(不拔掉我的耳机) - >调用音频路线更改回叫
- 然后我拔掉我的耳机(当声音仍在输出到扬声器时) - >未调用音频路线改变回调,这似乎是合乎逻辑的。
但这里是我的问题!所以我的问题是:你有没有办法检测到最后一种情况下拔掉了耳机?
But here is my problem ! So my question is : Do you see a way to detect that headphone was unplugged for this last case ?
感谢您的帮助
编辑:
好的我找到了解决方法:
Ok I found a workaround :
为了检测是否插入了耳机,我总是执行一个测试功能,我需要知道它(而不是使用布尔值),这可能对性能不太好但是它有效,这是我的代码,可能需要它:
To detect whether or not headphones are plugged, I execute a test function all the times I need to know it (instead using a boolean), this might be less good for performances but it's working, here is my code for the ones who may need it :
//set back the default audio route
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
//check if this default audio route is Heaphone or Speaker
CFStringRef newAudioRoute;
UInt32 newAudioRouteSize = sizeof(newAudioRoute);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &newAudioRouteSize, &newAudioRoute);
NSString *newAudioRouteString = (__bridge NSString *)newAudioRoute;
CFRelease(newAudioRoute);
//if this default audio route is not Headphone, it means no headphone is plugged
if ([newAudioRouteString rangeOfString:@"Headphones"].location != NSNotFound){
NSLog(@"Earphone available");
return true;
}
else {
NSLog(@"No Earphone available");
return false;
}
希望它会帮助别人!
推荐答案
这是解决此问题的最佳教程:
This is the best tutorial dealing this issue:
这篇关于音频路由不变时耳机插件插件事件 - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!