2010-10-06 34 views

回答

4

一些快速示例代碼,將做到這一點(當你的應用程序被激活或啓動&不要忘了鏈接到AVFoundation框架從什麼地方打電話):

#import <AVFoundation/AVAudioSession.h> 

// ... 

- (void)setupAudioSession 
{ 
    NSError* error = nil; 
    AVAudioSession* session = [AVAudioSession sharedInstance]; 
    // see documentation for delegate methods you should handle 
    [session setDelegate:self]; 
    // This category will duck and cancel background category, but can be configured 
    // later for mixing if you want (making it pretty versatile); see documentation 
    // on categories for other options 
    if(![session setCategory:AVAudioSessionCategoryPlayback error:&error]) { 
    // handle error 
    NSLog(@"Error setting audio category: %@, %@", error, [error userInfo]); 
    } 
    if(![session setActive:YES error:&error]) { 
    // handle error 
    NSLog(@"Error setting audio session as active: %@", error); 
    } 
} 
+0

你好謝謝回答我得到這樣的警告時,我用我們的代碼警告:「AVAudioSession」可以不響應「:withError:-SETACTIVE」和應用程序不再在模擬器退出後2秒運行。方案? – 2010-10-06 18:34:48

+0

@ Bobj-C:對不起,我是從記憶中打字。我將在代碼示例中修復它,但該方法是setActive:error :. – 2010-10-06 19:00:25

+0

警告是: 'AVAudioSession' 可能不響應 ':withError:-SETACTIVE'(![會議SETACTIVE:YES withError:&錯誤])這行,如果{ – 2010-10-06 19:13:43

2

如果配置和激活某些音頻會話類型在您的應用程序將播放聲音(見蘋果的音頻會話參考),操作系統將淡出,從任何背景聲音的應用程序當前正在使用的音頻輸出,讓您的應用程序將有可用的資源。

相關問題