任何帮助都非常感谢。请参阅底部的我的问题 (1) 和 (2)。
我是来自 Flex 开发的 Objective C 编程新手。我第一次尝试做一个简单的声音播放。我失败了,这是我在互联网上寻找答案时的常见问题。然而,我发现的所有解决方案似乎都不起作用。我只是想通过以下方法播放一个简短的 .caf 文件:
- (void) playUserInterfaceSoundEffect:(NSString *)file ofType:(NSString *)extension
{
NSLog(@"Call to playerUserInterfaceSoundEffect:%@ ofType:%@", file, extension);
SystemSoundID soundID = 0;
NSString *filePath = [NSString stringWithFormat:@"%@.%@", file, extension];
CFURLRef soundFileURL = (__bridge CFURLRef)[NSURL URLWithString:filePath];
OSStatus errorCode = AudioServicesCreateSystemSoundID(soundFileURL, &soundID);
NSLog(@"filePath: %@, soundFileURL: %@, errorCode: %ld", filePath, soundFileURL, errorCode);
if (errorCode != 0) NSLog(@"ERROR %ld: Failed to initialize UI audio file %@", errorCode, filePath);
AudioServicesPlaySystemSound(soundID);
}
我这样称呼它:
// Start with playing the audio effect for correct answer:
[self playUserInterfaceSoundEffect:@"answer_correct" ofType:@"caf"];
然而,声音并没有播放。尝试创建系统声音 ID 返回错误代码 -1500,如日志跟踪语句中所示:
2013-09-10 17:50:48.498 InFocus-dev[25888:c07] Call to playerUserInterfaceSoundEffect:answer_correct ofType:caf
2013-09-10 17:50:48.498 InFocus-dev[25888:c07] filePath: answer_correct.caf, soundFileURL: answer_correct.caf, errorCode: -1500
2013-09-10 17:50:48.498 InFocus-dev[25888:c07] ERROR -1500: Failed to initialize UI audio file answer_correct.caf
显然,错误代码是一个非常通用的错误代码,如下所述:(OSStatus) error = -1500 in AudioToolbox Framework -- 它代表 kAudioServicesSystemSoundUnspecifiedError 并且可能意味着音频文件的格式可能不正确或无法找到。
我的问题是:
(1) 音频文件的格式应该是正确的,但我怎样才能确定呢?这篇简短的帖子似乎表明某些文件可能会触发此错误,但尚不清楚是什么原因: http: //www.dosomethinghere.com/2009/05/05/audioservicescreatesystemsoundid-error-1500/
(2) 应用程序应该能够找到文件,但我如何确定?它们位于资源/资产/声音/。例如,可以毫无问题地找到位于资源/资产/图标/ 中的图像。如何测试应用程序是否根本找不到文件?
我觉得自己像个 N00b——感谢您的帮助!
埃里克
我是 StackOverflow 的新手,所以还不能回答我自己的问题,但这里是:
好的,正如@borrrden 指出的那样,答案是使用 NSBundle 获取完整的文件路径。
重写方法如下解决了这个问题:
- (void) playUserInterfaceSoundEffect:(NSString *)file ofType:(NSString *)extension
{
NSLog(@"Sound FX: Call to playerUserInterfaceSoundEffect:%@ ofType:%@", file, extension);
NSString *filePath = [[NSBundle mainBundle] pathForResource:file ofType:extension];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
CFURLRef inFileURL = (CFURLRef)CFBridgingRetain(fileURL);
SystemSoundID soundID;
OSStatus errorCode = AudioServicesCreateSystemSoundID(inFileURL, &soundID);
NSLog(@"\nfilePath:\t%@\nfileURL:\t%@\ninFileURL:\t%@", filePath, fileURL, inFileURL);
if (errorCode != 0) NSLog(@"ERROR %ld: Failed to initialize UI audio file %@", errorCode, filePath);
AudioServicesPlaySystemSound (soundID);
}
这个日志跟踪现在看起来像这样:
2013-09-10 19:58:56.165 InFocus-dev[27986:c07] Sound FX: Call to playerUserInterfaceSoundEffect:answer_correct ofType:caf
2013-09-10 19:58:56.165 InFocus-dev[27986:c07]
filePath: /Users/erik/Library/Application Support/iPhone Simulator/6.1/Applications/5A8B1CC8-2E32-4469-80C2-FA311242B5C5/InFocus-dev.app/answer_correct.caf
fileURL: file://localhost/Users/erik/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/5A8B1CC8-2E32-4469-80C2-FA311242B5C5/InFocus-dev.app/answer_correct.caf
inFileURL: file://localhost/Users/erik/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/5A8B1CC8-2E32-4469-80C2-FA311242B5C5/InFocus-dev.app/answer_correct.caf
松了口气..!
谢谢,埃里克