2012-07-18 195 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

相關問題