尝试播放音频时出错:998:无法设置声音,错误= -50

时间:2016-08-16 15:39:15

标签: ios swift audiotoolbox

当两个物体发生碰撞时,我正试图播放声音,每次都会出现此错误。我正在使用我的iPhone 6测试。这是代码:

    guard soundEffects[sound] == nil else
    {
        print("Sound effect \(sound) already exists")
        return false
    }

    guard let soundURL = NSBundle.mainBundle().URLForResource(sound, withExtension:format, subdirectory:path) else
    {
        print("Error creating URL for resource \(sound)")
        return false
    }

    print(soundURL)

    var systemID : SystemSoundID = id

    soundEffects[sound] = systemID
    print(AudioServicesCreateSystemSoundID(soundURL, &systemID))

    id += 1

我试图像这样玩:

    guard soundEffects[sound] != nil else
    {
        print("Sound effect '\(sound)' not found")
        return false
    }

    print(soundEffects[sound]!)

    AudioServicesPlaySystemSound(soundEffects[sound]!)

文件的格式是wav,我打印出创建方法的代码为0.我也似乎无法在苹果网站或网络上的任何其他地方找到关于错误的任何文档。我做错了吗?

更新

显然将值存储在字典中并在以后加载它会导致错误出现。但问题是我需要一本ID的字典。我该如何存储ID以及为什么存储它会导致错误?

1 个答案:

答案 0 :(得分:0)

我能够通过这样做来解决我的问题:

let systemID : SystemSoundID = someUInt32
soundEffects[sound] = systemID
AudioServicesCreateSystemSoundID(soundURL, &soundEffects[sound]!)
AudioServicesPlaySystemSound(soundEffects[sound]!)

似乎create方法执行其操作的对象是您可以调用play的唯一对象,因此您需要专门对字典实例(或者您尝试的任何对象)执行create方法存储声音。)