【问题标题】:How to debug error -1500 returned by AudioServicesCreateSystemSoundID?如何调试 AudioServicesCreateSystemSoundID 返回的错误 -1500?
【发布时间】:2013-09-13 23:04:28
【问题描述】:

非常感谢任何帮助。请参阅底部的我的问题 (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

松了一口气..!

谢谢, 埃里克

【问题讨论】:

  • 您需要文件的完整路径。从NSBundle获取它
  • 我问了这个问题,因为我花了一整天的时间寻找解决方案,但无济于事。从字面上看,在我发布此内容几分钟后,我发现......哇......从这里复制和粘贴解决方案可以让这一切正常工作:blog.logichigh.com/2009/02/14/… 现在我必须研究它并理解为什么......
  • @borrrden 是对的...我从上面粘贴的那个链接重写了解决方案,所以我可以更好地理解它,将其分解如下: NSString *filePath = [[NSBundle mainBundle] pathForResource:文件类型:扩展名]; NSURL *fileURL = [NSURL fileURLWithPath:filePath]; CFURLRef inFileURL = (CFURLRef)CFBridgingRetain(fileURL);我现在可以看到完整路径被追踪,并且当我这样做时正在播放声音: OSStatus errorCode = AudioServicesCreateSystemSoundID(inFileURL, &soundID); AudioServicesPlaySystemSound (soundID);非常感谢!

标签: ios objective-c audio caf


【解决方案1】:

我解决了一些问题以修复项目中的错误 -1500。

  1. 将音频剪辑修剪到 5 秒以下。我的 45 秒音频文件显然太长了。
  2. 将您的文件转换为 Apple 的核心音频格式 (.caf)
  3. 填写基本ID3元数据标签

我使用免费的Audacity 软件完成了所有这 3 个步骤。

将音频文件添加到项目时,请确保将文件复制到包的资源中(构建阶段 > 复制包资源)。

以下是我在 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)

【讨论】:

    猜你喜欢
    • 2017-04-14
    • 1970-01-01
    • 2012-04-06
    • 2011-09-16
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2019-11-11
    • 2016-07-08
    相关资源
    最近更新 更多