在 iOS 应用程序中使用现有的系统声音 [swift|

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31126124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:39:10  来源:igfitidea点击:

Using existing system sounds in iOS App [swift|

iosswiftaudioavfoundation

提问by TheOnlyXan

Is it possible to use the existing Apple system sounds in my own app? I would like to write a sample app in Swift that does the following steps:

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

  1. Read/Get a list of all available systemSounds on the device (I think they are located in /System/Library/Audio/UISounds/)
  2. show the list on the screen
  3. if I touch a list item, play these sound
  1. 读取/获取设备上所有可用系统声音的列表(我认为它们位于/System/Library/Audio/UISounds/
  2. 在屏幕上显示列表
  3. 如果我触摸列表项,播放这些声音

So its basically the same, like when you choose a new ringtone on your iPhone.

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

I think some apps are using this sounds, or have they copied/bought it?

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

Thanks and regards Jens

感谢并问候 Jens

回答by Beau Nouvelle

You can use this Swift 5 code to play system sounds:

您可以使用此 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)

The most up to date list of sounds I could find are here.

我能找到的最新声音列表在这里

Documentation Reference

文档参考

回答by Neha

Swift 4

斯威夫特 4

import AVFoundation

AudioServicesPlayAlertSound(SystemSoundID(1322))

回答by Derek Soike

Here's a quick way to test the sounds.

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

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)
}