0

我想要的是我的应用程序在进入后台时(按下主页按钮时)应该停止播放声音。我在 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);
4

1 回答 1

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>

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

于 2012-07-18T10:40:30.257 回答