回答
官方的iOS SDK沒有這樣做。想象一下,有人錯過了一個重要的電話,因爲應用程序改變了設置,並在沒有用戶的知識的情況下讓手機保持沉默我不想確定下載該應用程序。請參閱this相關問題。
從Apple的文檔
人,而不是應用程序,應啓動和控制操作。 雖然應用程序可以建議採取行動或警告有關危險的後果,但應用程序從用戶身上執行決策通常是錯誤的。最好的應用程序找到正確的 平衡給予他們所需的能力,同時幫助他們避免危險的結果。
如果我沒弄錯,讓手機沉默就是一種這樣的動作。
閱讀蘋果documentation的聲音部分。
編輯:如果你想了解更多信息。
前往apple developer forum(您必須先登錄),然後看看this的帖子。回答這個問題的人是一名蘋果員工。
// "Ambient" makes it respect the mute switch
// Must call this once to init session
if (!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.");
}
希望這將幫助你...
不,請嘗試這也沒有幫助。這不是我的正確解決方案 – triveni
你對,如果我們不處於環境模式,靜音開關是尊重的地方,這是行不通的。檢查下一個代碼,我發佈4 u ... – user755278
-(BOOL)muteSwitchEnabled {
#if TARGET_IPHONE_SIMULATOR
// set to NO in simulator. Code causes crashes for some reason.
return NO;
#endif
// go back to Ambient to detect the switch
AVAudioSession* 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) {
[sharedSession setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryError];
// By default PlayAndRecord plays out over the internal speaker. We want the external speakers, thanks.
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (ASRoute),
&ASRoute
);
}
else
// 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
[sharedSession setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
NSLog(@"Error setting audio category! %@", setCategoryError);
return muteSwitch;
}
第一開關到環境,讀取開關,然後返回到設置...
不要在線程中發佈多個答案...編輯您的原始帖子,如果你想改變 – Krishnabhadra
我只是分開寫了看清楚。我會在下次小心。謝謝 – user755278
你怎麼能像克里希那那樣說呢如果你不是真的那麼你爲什麼給我Upwote我只是幫你解決你的問題。 我接受downvote而不是刪除帖子。可能這會在未來幫助某人。 – user755278
- 1. 在Android中以編程方式設置沉默模式Phonegap
- 2. 以編程方式沉默/使用swift2振動iOS設備?
- 3. 我可以以編程方式沉默Rake任務嗎?
- 4. 如何以編程方式更改iPhone應用程序的默認圖標?
- 5. 以編程方式取消默認應用程序
- 6. 如何以編程方式更改默認應用程序?
- 7. 以編程方式打開設置應用程序(iPhone)
- 8. 可以通過編程方式關閉iphone應用程序
- 9. 以編程方式打開iphone日曆應用程序
- 10. 以編程方式創建Iphone應用程序vs Storyboard
- 11. 以編程方式卸載iPhone應用程序
- 12. 如何以編程方式退出iPhone應用程序?
- 13. 如何從iPhone應用程序中以編程方式傳真文檔?
- 14. 如何以編程方式從iPhone設備中刪除應用程序?
- 15. iPhone - 應用程序內購買的沉默
- 16. 以編程方式從內存中安裝應用程序
- 17. 從應用程序中以編程方式導出數據
- 18. 以編程方式從Windows上安裝iPhone上的應用程序
- 19. 以編程方式啓用/禁用沉浸模式
- 20. 以編程方式鎖定Iphone以編程方式
- 21. 以編程方式在越獄應用程序中編輯Info.plist
- 22. 以編程方式從C#應用程序安裝Apk文件
- 23. 以編程方式從wpf應用程序打開winform
- 24. 以編程方式從Android Play購買Android應用程序?
- 25. 以編程方式從Google Play下載應用程序
- 26. iPhone應用程序編程
- 27. 以編程方式安裝應用程序快捷方式
- 28. 如何以編程方式確定Linux中的默認應用程序
- 29. 如何在iPhone筆記應用程序中以編程方式創建筆記
- 30. 如何以編程方式在iPhone應用程序中打開LinkedIn Comapny Url?
爲每客戶r在按下無聲按鈕後,只有來電音量才能保持沉默。 – triveni
你想要什麼,你的應用去沉默(停止你的應用產生的所有聲音)或整個iPhone去沉默? – Krishnabhadra
第二個肯定是不可能的..蘋果不會肯定它.. – Krishnabhadra