Using existing system sounds in iOS App [swift|

感情迁移 提交于 2019-12-17 15:32:45

问题


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

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

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

Thanks and regards Jens


回答1:


You can use this Swift 4 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




回答2:


Swift 4

import AVFoundation

AudioServicesPlayAlertSound(SystemSoundID(1322))



回答3:


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


来源:https://stackoverflow.com/questions/31126124/using-existing-system-sounds-in-ios-app-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!