2010-05-01 300 views
2

作爲主題...有可能嗎?iphone,即使在靜音或靜音模式下如何播放聲音?

感謝

再次,我重視的代碼如下,請其步驟是錯誤的.thanks。

//@step 
AudioSessionInitialize (NULL, NULL, NULL, NULL); 
AudioSessionSetActive(true); 
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback; 
OSStatus error = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,           sizeof(sessionCategory),&sessionCategory); 

if (error) 
    printf("ERROR AudioSessionSetProperty ! %d\n", error); 

//@step 
NSString* filePath = @"AlarmClockBell.caf"; 
[Util restoreResourceFile:filePath]; 
filePath =[Util getFileFullPathFromSysDoc:filePath]; 
NSURL *soundFileURL = [NSURL fileURLWithPath:filePath]; 
NSError* error ; 
AVAudioPlayer * audioPalyer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: &error]; 
if (nil == audioPalyer) 
{ 
    AppTrace3(self, @"Faild to play", soundFileURL, error); 
    return FALSE; 
} 
[audioPalyer prepareToPlay]; 
[audioPalyer setVolume: 5 ]; 
[audioPalyer setDelegate: self]; 
audioPalyer.numberOfLoops = 10; 

[audioPalyer play]; 

感謝...

+0

此代碼是否最終工作?您是否解決了您的問題? – 2011-03-18 20:46:18

回答

0

它必須是,有已經有幾場比賽我有哪些(即使它在靜音模式)將播放聲音。不幸的是,這是在嘗試在課堂上祕密玩遊戲時發現的。

至於如何真正做到這一點,我真的不知道。

+0

嗨,保羅,謝謝你的回答,遊戲是否來自AppStore?我找到了一些解決方案,但它似乎只適用於解鎖iPhone ... – iXcoder 2010-05-01 08:51:06

6

如果您查看音頻會話類別下的文檔,您會發現可設置的多種模式,以告知系統您的應用計劃如何使用音頻。默認值爲AVAudioSessionCategorySoloAmbient,用於跟蹤鈴聲/靜音開關和屏幕鎖定。

讓自己的應用忽略鈴聲/靜音開關的設置,你可以嘗試改變類別:

#import <AudioToolbox/AudioToolbox.h> 

AudioSessionInitialize (NULL, NULL, NULL, NULL); 
AudioSessionSetActive(true); 

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback; 
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, 
         sizeof(sessionCategory),&sessionCategory); 

如果你想允許iPod的音頻繼續在後臺播放,您還需要檢查kAudioSessionProperty_OverrideCategoryMixWithOthers

+0

嗨,拉敏,非常感謝你的答案,這是非常有益的,但我嘗試做的方式,它仍然沒有工作正常.. iPhone設備的音量爲靜音(音量調整爲零),當代碼運行時,它仍然沒有聲音,除非我通過手動調節音量... 我的代碼如下: – iXcoder 2010-05-01 13:32:27

+0

對不起,這很難看....:{ – iXcoder 2010-05-01 13:36:00

+0

嗨,Ramin,我在上面的問題中添加了代碼,請幫我檢查一下,如果你有時間的話...謝謝! – iXcoder 2010-05-01 14:11:05

1

從iOS 6開始,有一種比Ramin's answer更緊湊的替代方法。

#import <AVFoundation/AVFoundation.h> 

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
             error:nil]; 

要允許從其他應用背景音樂繼續播放,添加AVAudioSessionCategoryOptionMixWithOthers選項:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
           withOptions:AVAudioSessionCategoryOptionMixWithOthers 
             error:nil]; 

有蘋果的AVAudioSession Class Reference進一步的細節。

相關問題