2011-09-05 45 views

回答

6

官方的iOS SDK沒有這樣做。想象一下,有人錯過了一個重要的電話,因爲應用程序改變了設置,並在沒有用戶的知識的情況下讓手機保持沉默我不想確定下載該應用程序。請參閱this相關問題。

從Apple的文檔

人,而不是應用程序,應啓動和控制操作。 雖然應用程序可以建議採取行動或警告有關危險的後果,但應用程序從用戶身上執行決策通常是錯誤的。最好的應用程序找到正確的 平衡給予他們所需的能力,同時幫助他們避免危險的結果。

如果我沒弄錯,讓手機沉默就是一種這樣的動作。

閱讀蘋果documentation的聲音部分。

編輯:如果你想了解更多信息。

前往apple developer forum(您必須先登錄),然後看看this的帖子。回答這個問題的人是一名蘋果員工。

+0

爲每客戶r在按下無聲按鈕後,只有來電音量才能保持沉默。 – triveni

+0

你想要什麼,你的應用去沉默(停止你的應用產生的所有聲音)或整個iPhone去沉默? – Krishnabhadra

+0

第二個肯定是不可能的..蘋果不會肯定它.. – Krishnabhadra

2
// "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."); 
} 

希望這將幫助你...

+0

不,請嘗試這也沒有幫助。這不是我的正確解決方案 – triveni

+0

你對,如果我們不處於環境模式,靜音開關是尊重的地方,這是行不通的。檢查下一個代碼,我發佈4 u ... – user755278

0

沒有公開的API開放給開發者,因爲當你的應用程序正在運行,接到一個電話,那麼你的應用程序將退出,也可以在後臺但你水溼做出裝置。因爲通話的任何變化也對系統級事件做出

+0

我必須創建的應用程序,其中iphone得到無聲點擊事件iphone去沉默配置文件模式... – triveni

+0

但通過這樣做的結果將是一個應用程序有私人框架和蘋果將​​拒絕該應用程序。如果仍然需要它,那麼請閱讀如何在iphone中解鎖私有框架(ERICA SADUN),以幫助您使用可能爲您提供此功能的私有框架。 – Ballu

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

第一開關到環境,讀取開關,然後返回到設置...

+0

不要在線程中發佈多個答案...編輯您的原始帖子,如果你想改變 – Krishnabhadra

+0

我只是分開寫了看清楚。我會在下次小心。謝謝 – user755278

+0

你怎麼能像克里希那那樣說呢如果你不是真的那麼你爲什麼給我Upwote我只是幫你解決你的問題。 我接受downvote而不是刪除帖子。可能這會在未來幫助某人。 – user755278

相關問題