【问题标题】:Playing system sounds using Swift使用 Swift 播放系统声音
【发布时间】:2014-12-22 10:13:26
【问题描述】:

我正在尝试使用系统声音在按下按钮时播放声音。我不断收到错误消息:“致命错误:在展开可选值时意外发现 nil”。我尝试调试,我相信错误发生在 var ref 行上。 “roar.aif”是我在项目中包含的自定义声音。它只有一秒半长。我会很感激建议。谢谢!

        var soundID: SystemSoundID = SystemSoundID()
        var mainBundle: CFBundleRef = CFBundleGetMainBundle()
        var ref: CFURLRef = CFBundleCopyResourceURL(mainBundle, "roar.aif", nil, nil) as CFURLRef!
        println("Here is your ref: \(ref)")
        AudioServicesCreateSystemSoundID(ref, &soundID)
        AudioServicesPlaySystemSound(soundID)

【问题讨论】:

    标签: ios swift system-sounds


    【解决方案1】:

    我相信正确的方法是在第三个参数中使用filetype:

        var soundID: SystemSoundID = 0
        var mainBundle: CFBundleRef = CFBundleGetMainBundle()
        if let ref: CFURLRef = CFBundleCopyResourceURL(mainBundle, CFSTR("roar"), CFSTR("aif"), nil) {
            println("Here is your ref: \(ref)")
            AudioServicesCreateSystemSoundID(ref, &soundID)
            AudioServicesPlaySystemSound(soundID)
        } else {
            println("Could not find sound file")
        }
    

    您确定文件是 roar.aif 而不是 roar.aiff?

    确保文件不在子目录中,如果是,则必须将其添加为第四个参数。

    见:https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFBundleRef/index.html#//apple_ref/c/func/CFBundleCopyResourceURL

    【讨论】:

    • 这工作......有点。它打印出它找不到声音文件,尽管我的声音文件在我的项目导航器中显示为 roar.aif...有什么想法吗?
    • 检查目标的“构建阶段”。在“复制捆绑资源”部分中,该文件 roar.aif 应该存在。
    • 解决了!太感谢了!你帮了大忙!
    【解决方案2】:

    为了扩展 Tom 的答案(完全正确),这里有一个更强大的错误处理程序(这将有助于平息 Apple 的应用程序测试人员)

    else {
            let networkIssueController = UIAlertController(title: "Error", message: "Unable to find an audio file!", preferredStyle: .Alert)
            let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
            networkIssueController.addAction(okButton)
    
            let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
            networkIssueController.addAction(cancelButton)
    
            self.presentViewController(networkIssueController, animated: true, completion: nil)// you have to display the alert after you create it
    

    }

    【讨论】:

      【解决方案3】:

      Swift 2.0

      func createSystemSoundID() -> SystemSoundID {
          var soundID: SystemSoundID = 0
          let soundURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), "roar", "aiff", nil)
          AudioServicesCreateSystemSoundID(soundURL, &soundID)
          return soundID
      }
      

      创建一次:

      let systemSoundID = createSystemSoundID()
      

      随意调用:

      AudioServicesPlaySystemSound(systemSoundID)
      

      【讨论】:

        【解决方案4】:

        斯威夫特 4.1

        if let path = Bundle.main.path(forResource: "roar", ofType: "aif") {
            let url = URL(fileURLWithPath: path) as CFURL
            var soundID: SystemSoundID = 0
            AudioServicesCreateSystemSoundID(url, &soundID)
            AudioServicesPlaySystemSound(soundID)
        } else {
            print("file not found!")
        }
        

        【讨论】:

          猜你喜欢
          • 2010-11-12
          • 1970-01-01
          • 1970-01-01
          • 2012-09-07
          • 1970-01-01
          • 2023-02-25
          • 2017-10-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多