我有一個應用程序使用AudioServicesPlaySystemSound(1104)方法在某些輕擊事件上播放聲音。它響應靜音模式與非靜音模式的狀態,並相應地靜音。檢測iOS中的靜音模式狀態
然而,AudioServicesPlaySystemSound(1150)處於什麼狀態振動開關是英寸
如何檢查的振動模式的狀態,這樣當用戶期望它,我可以沉默的聲音會不管玩?
謝謝
我有一個應用程序使用AudioServicesPlaySystemSound(1104)方法在某些輕擊事件上播放聲音。它響應靜音模式與非靜音模式的狀態,並相應地靜音。檢測iOS中的靜音模式狀態
然而,AudioServicesPlaySystemSound(1150)處於什麼狀態振動開關是英寸
如何檢查的振動模式的狀態,這樣當用戶期望它,我可以沉默的聲音會不管玩?
謝謝
似乎有一個開源項目可以做到這一點。見here for the code and a sample project。 RBDMuteSwitch
是這裏的關鍵課程。您可以使用它(作爲單身人士),並將您的班級設置爲RDBMuteSwitch
的代表,以獲知有關交換機狀態的通知。您將收到開關變更的通知,或者您第一次撥打[[RBDMuteSwitch sharedInstance] detectMuteSwitch]
。經過測試工作在iOS 9
這裏的RBDMuteSwitch.m的重要內容,以防GitHub的鏈接永遠不會消逝:
- (void)playbackComplete {
if ([(id)self.delegate respondsToSelector:@selector(isMuted:)]) {
// If playback is far less than 100ms then we know the device is muted
if (soundDuration < 0.010) {
[delegate isMuted:YES];
}
else {
[delegate isMuted:NO];
}
}
[playbackTimer invalidate];
}
static void soundCompletionCallback (SystemSoundID mySSID, void* myself) {
AudioServicesRemoveSystemSoundCompletion (mySSID);
[[RBDMuteSwitch sharedInstance] playbackComplete];
}
- (void)incrementTimer {
soundDuration = soundDuration + 0.001;
}
- (void)detectMuteSwitch {
#if TARGET_IPHONE_SIMULATOR
// The simulator doesn't support detection and can cause a crash so always return muted
if ([(id)self.delegate respondsToSelector:@selector(isMuted:)]) {
[self.delegate isMuted:YES];
}
return;
#endif
#if __IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
// iOS 5+ doesn't allow mute switch detection using state length detection
// So we need to play a blank 100ms file and detect the playback length
soundDuration = 0.0;
CFURLRef soundFileURLRef;
SystemSoundID soundFileObject;
// Get the main bundle for the app
CFBundleRef mainBundle = CFBundleGetMainBundle();
// Get the URL to the sound file to play
soundFileURLRef = CFBundleCopyResourceURL(mainBundle,
CFSTR ("detection"),
CFSTR ("aiff"),
NULL);
// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID (soundFileURLRef,
&soundFileObject);
AudioServicesAddSystemSoundCompletion (soundFileObject,NULL,NULL,
soundCompletionCallback,
(void*) self);
// Start the playback timer
playbackTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(incrementTimer) userInfo:nil repeats:YES];
// Play the sound
AudioServicesPlaySystemSound(soundFileObject);
return;
#else
// This method doesn't work under iOS 5+
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0) {
if ([(id)self.delegate respondsToSelector:@selector(isMuted:)]) {
[self.delegate isMuted:NO];
}
}
if ([(id)self.delegate respondsToSelector:@selector(isMuted:)]) {
[self.delegate isMuted:YES];
}
return;
#endif
}
從本質上講,該代碼確實是一種無聲的聲音片段通話AudioServicesPlaySystemSound()
已知的持續時間,並測試玩多久。如果播放持續時間少於預期時間,則表示靜音開關已打開。
這是不適用於最新的iOS版本,如iOS 8,9。 –
@AnilkumariOSdeveloper,我在iOS 9(.0)上測試它。您的項目中可能還有其他問題。無法僅以「這不起作用」告知。 – Nate