无法播放系统声音iOS 7

时间:2014-03-18 11:27:54

标签: ios iphone objective-c audio audiotoolbox

在我的应用程序中,当收到特定频率的信号时,我将播放系统声音。为了检测频率,我使用了PitchDetector。然后我看了互联网,看看如何播放iOS系统声音,我发现this。我想播放名为sms_alert_circles.caf的声音,所以在方法- (void)viewDidLoad中我写了这段代码:

NSURL *fileURL = [NSURL URLWithString:@"/System/Library/Audio/UISounds/Modern/sms_alert_circles.caf"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)fileURL,&soundID);

然后在检测音频信号频率的方法中我把这段代码:

AudioServicesPlaySystemSound(soundID);

但如果我尝试在我的iPhone上运行此代码,则没有任何反应。它识别频率,但不执行声音,我的代码有什么问题?你能救我吗?

显然我打开了设备的声音

1 个答案:

答案 0 :(得分:2)

也许你的路径是错误的,它应该是应用程序包内文件的路径。

将您的sms_alert_circles.caf文件添加到您的xcode项目中。检查该文件是否位于项目“Build Phases”的“Copy Bundle Resources”列表中。然后更改以下行:

NSURL *fileURL = [NSURL URLWithString:@"/System/Library/Audio/UISounds/Modern/sms_alert_circles.caf"];

NSString *soundPath = [[NSBundle mainBundle]
                           pathForResource:@"sms_alert_circles.caf"
                           ofType:nil];
NSURL *fileURL = [NSURL URLWithString:soundPath];

此外,您可以尝试使用AKSystemSound库通过几行代码播放声音。