gpt4 book ai didi

ios - 如何调试AudioServicesCreateSystemSoundID返回的错误-1500?

转载 作者:行者123 更新时间:2023-12-01 16:47:36 26 4
gpt4 key购买 nike

任何帮助真的很感激。请参阅底部的问题(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)应用程序应该能够找到文件,但是如何确定?它们位于资源/ Assets /声音/中。例如,找到位于资源/ Assets /图标/中的图像没有问题。如何测试应用程序是否根本找不到文件?

我感觉像是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

太松了..!

谢谢,
埃里克

最佳答案

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

  • 将音频片段修剪到5秒以内。我的45秒音频文件显然太长。
  • 将文件转换为Apple的核心音频格式(.caf)
  • 填写基本的ID3元数据标签

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

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

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

    关于ios - 如何调试AudioServicesCreateSystemSoundID返回的错误-1500?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18715502/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com