gpt4 book ai didi

ios - 耳机插好了吗? IOS 7

转载 作者:IT王子 更新时间:2023-10-29 07:33:40 26 4
gpt4 key购买 nike

为 iPhone 开发一个应用程序,其中包含需要通过耳机收听的音频文件。

如何检查耳机是否未插入以便告诉用户插入耳机。

我有来自另一个线程的以下代码,但不推荐使用 audioSessionGetProperty 方法。任何人都知道如何更改以下代码以使其工作或拥有自己的代码/解决方案。

谢谢。

- (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;
}

最佳答案

这应该可以,但我现在无法测试,我会在晚上进行。

- (BOOL)isHeadsetPluggedIn {
AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
for (AVAudioSessionPortDescription* desc in [route outputs]) {
if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
return YES;
}
return NO;
}

关于ios - 耳机插好了吗? IOS 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21292586/

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