ios - 检测iOS中静音模式的状态
<p><p>我有一个应用程序使用 AudioServicesPlaySystemSound(1104) 方法在某些点击事件上播放声音。它响应静音模式和非静音模式的状态,并相应地静音。</p>
<p>但是,无论振动开关处于什么状态,AudioServicesPlaySystemSound(1150) 都会播放。</p>
<p>如何检查振动模式的状态,以便在用户期望的时候静音?</p>
<p>谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>似乎有一个开源项目可以做到这一点。见 <a href="https://github.com/Rich2k/RBDMuteSwitch" rel="noreferrer noopener nofollow">here for the code and a sample project</a> . <code>RBDMuteSwitch</code> 是这里的关键类。您使用它(作为单例),并将您的类设置为 <code>RDBMuteSwitch</code> 的委托(delegate),以获取有关开关状态的通知。您将在开关 <strong>更改</strong> 或第一次调用 <code>[ detectMuteSwitch]</code> 时收到通知。在 iOS 9 上测试。</p>
<p>这是 RBDMuteSwitch.m 的重要内容,以防 github 链接失效:</p>
<pre><code>- (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) {
;
}
else {
;
}
}
;
}
static void soundCompletionCallback (SystemSoundID mySSID, void* myself) {
AudioServicesRemoveSystemSoundCompletion (mySSID);
[ 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:)]) {
;
}
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 = ;
// 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:)]) {
;
}
}
if ([(id)self.delegate respondsToSelector:@selector(isMuted:)]) {
;
}
return;
#endif
}
</code></pre>
<p>从本质上讲,代码所做的是调用 <code>AudioServicesPlaySystemSound()</code> 以获得已知持续时间的无声剪辑,并测试播放需要多长时间。如果播放持续时间小于预期时间,则表示静音开关已打开。</p></p>
<p style="font-size: 20px;">关于ios - 检测iOS中静音模式的状态,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34699153/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34699153/
</a>
</p>
页:
[1]