【问题标题】:Using existing system sounds in iOS App [swift|在 iOS 应用中使用现有系统声音 [swift|
【发布时间】:2015-09-16 12:47:20
【问题描述】:

是否可以在我自己的应用程序中使用现有的 Apple 系统声音?我想用 Swift 编写一个示例应用程序,它执行以下步骤:

  1. 读取/获取设备上所有可用系统声音的列表(我认为它们位于 /System/Library/Audio/UISounds/
  2. 在屏幕上显示列表
  3. 如果我触摸列表项,播放这些声音

所以它基本上是一样的,就像你在你的 iPhone 上选择一个新的铃声一样。

我认为某些应用正在使用这种声音,或者他们是否复制/购买了它?

感谢和问候 延斯

【问题讨论】:

  • 值得注意的是/System/Library/Audio/UISounds/存在于物理设备上但不存在于模拟器中。

标签: ios swift audio avfoundation


【解决方案1】:

您可以使用此 Swift 5 代码播放系统声音:

// import this
import AVFoundation

// create a sound ID, in this case its the tweet sound.
let systemSoundID: SystemSoundID = 1016

// to play sound
AudioServicesPlaySystemSound(systemSoundID)

我能找到的最新声音列表是here

Documentation Reference

这就是它们听起来的样子: https://www.youtube.com/watch?v=TjmkmIsUEbA

【讨论】:

  • Apple 记录在哪里?
  • Apple 是否有关于开发人员何时可以使用 iOS 系统声音的指南?
  • 如果您导入AVFoundation,您可以在一行中完成剩下的工作:AudioServicesPlaySystemSound(SystemSoundID(1016))
  • 我注意到这不是超级可靠...我调用此代码并同时打印到控制台。通常它会打印到控制台,但不会播放声音。似乎随机失败。
  • 根据 AudioServicesPlaySystemSound 的文档:“在使用此函数之前,请调用 AudioServicesCreateSystemSoundID(::) 函数以获取系统声音。”在 AudioServicesPlayAlertSound 中也提到:“系统提供的警报声音和系统提供的用户界面声音效果对您的 iOS 应用程序不可用。”
【解决方案2】:

这是测试声音的快速方法。

import AVFoundation

func displaySoundsAlert() {
    let alert = UIAlertController(title: "Play Sound", message: nil, preferredStyle: UIAlertController.Style.alert)
    for i in 1000...1010 {
        alert.addAction(UIAlertAction(title: "\(i)", style: .default, handler: {_ in
            AudioServicesPlayAlertSound(UInt32(i))
            self.displaySoundsAlert()
        }))
    }
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(alert, animated: true, completion: nil)
}

【讨论】:

    【解决方案3】:

    斯威夫特 4

    import AVFoundation
    
    AudioServicesPlayAlertSound(SystemSoundID(1322))
    

    【讨论】:

    猜你喜欢
    • 2014-12-22
    • 2011-09-11
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    相关资源
    最近更新 更多