Using existing system sounds in iOS App [swift|

后端 未结 3 682
北荒
北荒 2020-12-04 15:51

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:

  1. Read/Get a lis
相关标签:
3条回答
  • 2020-12-04 16:05

    Swift 4

    import AVFoundation
    
    AudioServicesPlayAlertSound(SystemSoundID(1322))
    
    0 讨论(0)
  • 2020-12-04 16:06

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

    // 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

    0 讨论(0)
  • 2020-12-04 16:14

    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)
    }
    
    0 讨论(0)
提交回复
热议问题