任何帮助真的很感激。请在底部查看我的问题(1)和(2)。
我是Objective C编程的新手,来自Flex开发。我第一次尝试做一个简单的声音播放。我失败了,当我在互联网上寻找答案时,这是一个常见的问题。但是,我发现的解决方案似乎都没有效果。我只是想通过以下方法播放一个简短的.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
松了一口气......!
谢谢, 埃里克
答案 0 :(得分:0)
我解决了一些问题,以解决项目中的-1500错误。
我使用免费的Audacity软件完成了所有这三个步骤。
将音频文件添加到项目时,请确保将文件复制到捆绑包的资源中(“构建阶段”>“复制捆绑包资源”)。
以下是我在iOS 12.1.2上用于Swift 4.2的工作代码。
import AudioToolbox
var soundID: SystemSoundID = 0
guard let audioFileURL = Bundle.main.url(forResource: "your_audio_file", withExtension: "caf") else {
print("Unable to find audio file.")
return
}
AudioServicesCreateSystemSoundID(audioFileURL as CFURL, &soundID)
AudioServicesAddSystemSoundCompletion(soundID, nil, nil, { soundID, _ in
AudioServicesDisposeSystemSoundID(soundID)
}, nil)
AudioServicesPlaySystemSound(soundID)