我一直在使用来自infragistics的iOS 7扫描条码(Objective-C),效果很好。我正在尝试在完成成功扫描时添加一声蜂鸣声,但一直收到错误。对于比我更了解的人,我确信这是一个简单的解决方法。
-(void)loadBeepSound{
// Get the path to the beep.mp3 file and convert it to a NSURL object.
NSString *beepFilePath = [NSString stringWithFormat:@"%@/beep.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *beepURL = [NSURL URLWithString:beepFilePath];
NSError *error;
// Initialize the audio player object using the NSURL object previously set.
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:beepURL error:&error];
if (error) {
// If the audio player cannot be initialized then log a message.
NSLog(@"Could not play beep file.");
NSLog(@"%@", [error localizedDescription]);
NSLog(@"beepFilePath %@", beepFilePath);
}
else{
// If the audio player was successfully initialized then load it in memory.
[_audioPlayer prepareToPlay];
}
}
错误记录为 -
-Could not play beep file. -The operation couldn’t be completed. (OSStatus error -10875.) -beepFilePath /private/var/mobile/Containers/Bundle/Application/22BB6164-82F8-46E5-B4B6- 4C91BCA2B7E9/ReThink Edu App.app/beep.mp3
因此加载哔声似乎是一个问题,但它在主束中并且目标成员资格框被勾选。也许我只是想以错误的方式解决这个问题!
答案 0 :(得分:2)
而不是:
NSURL *beepURL = [NSURL URLWithString:beepFilePath];
请改为尝试:
NSURL *beepURL = [NSURL fileURLWithPath:beepFilePath];
答案 1 :(得分:0)
使用[NSURL URLWithString:beepFilePath];
时,它只获取文件夹路径的字符串,如Users/***/folder/etc
。
因此请使用[NSURL fileURLWithPath:beepFilePath];
此代码作为完整路径,例如file:///Users/***/folder/etc
。
工作代码:
NSURL *beepURL = [NSURL fileURLWithPath:beepFilePath];
可选方法:
NSURL *beepURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:///%@", beepFilePath]];