我能否在我的应用程序中使用现有的苹果系统声音? 我想用Swift编写一个简单的应用程序,完成以下步骤:
- 读取 / 获取设备上所有可用系统声音的列表(我认为它们位于
/System/Library/Audio/UISounds/
) - 在屏幕上显示列表
- 如果我点击了列表项,则播放这些声音
所以它基本上与您在 iPhone 上选择新铃声时相同。
我认为某些应用程序正在使用这些声音,或者他们是复制/购买的吗?
我能否在我的应用程序中使用现有的苹果系统声音? 我想用Swift编写一个简单的应用程序,完成以下步骤:
/System/Library/Audio/UISounds/
)所以它基本上与您在 iPhone 上选择新铃声时相同。
我认为某些应用程序正在使用这些声音,或者他们是复制/购买的吗?
你可以使用这段 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)
我能找到的最新声音列表在这里。
以下是它们的所有声音示例: https://www.youtube.com/watch?v=TjmkmIsUEbA
AVFoundation
,则可以在一行中完成其余操作:AudioServicesPlaySystemSound(SystemSoundID(1016))
。 - peacetypeSwift 4
import AVFoundation
AudioServicesPlayAlertSound(SystemSoundID(1322))
这是一种快速测试声音的方法。
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)
}
并且使用带有完成回调的版本,正如苹果所说:
此功能将在未来的版本中被弃用。请改用AudioServicesPlayAlertSoundWithCompletion或AudioServicesPlaySystemSoundWithCompletion。
所以:
AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, nil)
/System/Library/Audio/UISounds/
存在于物理设备上,但在模拟器中不存在。 - Derek Soike