我在 stackoverflow 上找到了很多解决方案,但由于某种原因,它们都未能产生任何声音。
这是我当前的代码:
PRE: I've added AudioToolbox.framework and imported <AVFoundation/AVFoundation.h> in my .m file
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL* sndurl = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"aif"];
SystemSoundID snd;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)sndurl, &snd);
AudioServicesAddSystemSoundCompletion(snd, nil, nil, SoundFinished, nil);
AudioServicesPlaySystemSound(snd);
}
void SoundFinished (SystemSoundID snd, void* context) {
AudioServicesRemoveSystemSoundCompletion(snd);
AudioServicesDisposeSystemSoundID(snd);
}
这个解决方案是完全出书的。但是当我运行项目时,如果无法产生任何声音。我的文件是 test.aif,长度为 5 秒。
有什么建议为什么它不起作用?如果这不是最好的解决方案,你如何在 iOS6 上产生声音?