耳机插入了吗? IOS 7 [英] Are headphones plugged in? iOS7
本文介绍了耳机插入了吗? IOS 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
开发适用于iPhone的应用程序,其中包含需要通过耳机收听的音频文件。
Developing an app for an iPhone with audio files that need to be listened too through headphones.
如何检查耳机是否未插入,以便我可以告诉用户插入耳机。
How do I check if headphones aren't plugged in so I can tell the user to plug in headphones.
我有来自另一个线程的以下代码,但不推荐使用audioSessionGetProperty方法。任何人都知道如何更改以下代码以使其工作或有自己的代码/解决方案。
I have the following code from another thread but the audioSessionGetProperty method is deprecated. Anyone know how to alter the following code to make this work OR have there own code/solution.
谢谢。
- (BOOL)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
//Maybe changing it to something like the following would work for iOS7?
//AVAudioSession* session = [AVAudioSession sharedInstance];
//OSStatus error = [session setCategory:kAudioSessionProperty_AudioRoute...?
//the line below is whats giving me the warning
OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);
/* Known values of route:
* "Headset"
* "Headphone"
* "Speaker"
* "SpeakerAndMicrophone"
* "HeadphonesAndMicrophone"
* "HeadsetInOut"
* "ReceiverAndMicrophone"
* "Lineout"
*/
if (!error && (route != NULL)) {
NSString* routeStr = (__bridge NSString*)route;
NSRange headphoneRange = [routeStr rangeOfString : @"Head"];
if (headphoneRange.location != NSNotFound) return YES;
}
return NO;
}
推荐答案
这应该有效,但我我现在不能测试,我会在晚上做。
This should work, but I cannot test it right now, I'll do in the evening.
- (BOOL)isHeadsetPluggedIn {
AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
for (AVAudioSessionPortDescription* desc in [route outputs]) {
if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
return YES;
}
return NO;
}
这篇关于耳机插入了吗? IOS 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文