2010-10-06 53 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

如果配置和激活某些音频会话类型在您的应用程序将播放声音(见苹果的音频会话参考),操作系统将淡出,从任何背景声音的应用程序当前正在使用的音频输出,让您的应用程序将有可用的资源。

相关问题