猿问

如何以编程方式感知iPhone静音开关?

如何以编程方式感知iPhone静音开关?

我似乎无法在SDK中找到如何以编程方式感知iPhone上的静音按钮/开关。当我的应用程序播放背景音乐时,它会正确响应音量按钮,而我没有任何代码可以遵循,但是,当我使用静音开关时,它只是继续播放。

如何测试静音的位置?

(注意:我的程序有自己的静音开关,但我希望物理开关覆盖它。)


慕工程0101907
浏览 613回答 3
3回答

桃花长相依

JPM。实际上,你提供的链接会得到正确的答案(最终。;)为了完整性(因为SO应该是QUICK答案的来源!)......// "Ambient" makes it respect the mute switch// Must call this once to init sessionif (!gAudioSessionInited){     AudioSessionInterruptionListener    inInterruptionListener = NULL;     OSStatus    error;     if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))     {         NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error);     }     else     {         gAudioSessionInited = YES;     }}SInt32  ambient = kAudioSessionCategory_AmbientSound;if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient)){     NSLog(@"*** Error *** could not set Session property to ambient.");}

GCT1015

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

噜噜哒

如果您不在静音模式下,其他答案中的某些代码(包括接受的答案)可能不起作用,其中静音开关受到尊重。我编写了下面的例程切换到环境,读取开关,然后返回到我的应用程序中需要的设置。-(BOOL)muteSwitchEnabled {#if TARGET_IPHONE_SIMULATOR&nbsp; &nbsp; // set to NO in simulator. Code causes crashes for some reason.&nbsp; &nbsp; return NO;#endif// go back to Ambient to detect the switchAVAudioSession* sharedSession = [AVAudioSession sharedInstance];[sharedSession setCategory:AVAudioSessionCategoryAmbient error:nil];CFStringRef state;UInt32 propertySize = sizeof(CFStringRef);AudioSessionInitialize(NULL, NULL, NULL, NULL);AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);BOOL muteSwitch = (CFStringGetLength(state) <= 0);NSLog(@"Mute switch: %d",muteSwitch);// code below here is just restoring my own audio state, YMMV_hasMicrophone = [sharedSession inputIsAvailable];NSError* setCategoryError = nil;if (_hasMicrophone) {&nbsp; &nbsp; [sharedSession setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryError];&nbsp; &nbsp; // By default PlayAndRecord plays out over the internal speaker.&nbsp; We want the external speakers, thanks.&nbsp; &nbsp; UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;&nbsp; &nbsp; AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sizeof (ASRoute),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&ASRoute&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);}else&nbsp; &nbsp; // Devices with no mike don't support PlayAndRecord - we don't get playback, so use just playback as we don't have a microphone anyway&nbsp; &nbsp; [sharedSession setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];if (setCategoryError)&nbsp; &nbsp; NSLog(@"Error setting audio category! %@", setCategoryError);return muteSwitch;}
随时随地看视频慕课网APP
我要回答