2012-07-18 112 views
0

我想要的是当我的应用程序进入后台(当按下home按钮时)应该停止播放声音。我这样做的appDelegate.m但它说use of undeclared identifier 'soundID'当应用程序进入后台时处理事件

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    AudioServicesDisposeSystemSoundID(soundID) 
} 

我已经导入我的VC到代表和也暴露了soundID作为属性。我哪里错了?请帮忙。

NSString *path = [[NSBundle mainBundle] pathForResource:@"something" ofType:@"wav"]; 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID); 
    AudioServicesPlaySystemSound(soundID); 

回答

0

如果您想要的功能,你应该去你的应用程序主要plist文件(可能命名为[your-app-name]-Info.plist),并在那里找到了关键:

Required background modes 

,并删除值

App plays audio 

这应该做的伎俩。

---------------- 编辑:

试试这个:

NSString* path = [[NSBundle mainBundle] pathForResource:@"myWavFile" ofType:@"wav" inDirectory:@"DirectoryName"]; 
self.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]]; 
[self.player play]; 

哪里自我的球员是:

@property (strong, nonatomic) AVPlayer *player; 

您还应该:

#import <AVFoundation/AVFoundation.h> 

并将此框架添加到您的项目中。

+0

“必需的背景模式”键已经丢失。 – 2012-07-18 10:46:33

+0

然后音频不应该在后台模式下播放!请发布您的代码如何播放音频。 – Kuba 2012-07-18 10:58:49

+0

我正在使用短系统声音播放音频。我将编辑我的问题。 – 2012-07-18 11:07:37

相关问题