@Jessica,你不能这样做,因为它受到限制。如果您想在您的应用程序中尝试它,那么您的应用程序可能会在 App Store 中被拒绝。
因此,使用公共 API 是不可能的。
您找到的链接使用的是私有 API,这些 API 没有记录或保证按您期望的方式工作。如果您尝试发布调用私有 API 的 App Store 应用程序,它将被自动拒绝。
如果你想检查,是否沉默,然后使用下面的代码,
-(BOOL)silenced {
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
return NO;
else
return YES;
}
For completeness, building off this link from Dan Bon, I implement the following method to solve this problem in my apps. One thing to note is that the code checks for the iPhone simulator first - executing the below code will crash the simulator. Anyone know why?
-(BOOL)silenced {
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
return NO;
else
return YES;
}
在视图控制器中声明此权利,您只需检查
if ([self silenced]) {
NSLog(@"silenced");
else {
NSLog(@"not silenced");
}