Swift-use of system sound services (play sounds, reminders, vibrations)

Source: Internet
Author: User

1, System Sound Service Introduction:

The System Sound service provides an API to play sounds for no more than 30 seconds. It supports file formats that are limited, specifically only CAF, AIF, and WAV files that use PCM or IMA/ADPCM data. However, this function does not provide the ability to manipulate sounds and control the volume, so do not use the system sound service if you are creating specialized sounds for multimedia or games.
2, the system sound service supports the following three types:(1) Sound: Instantly play a simple sound file. If the phone is muted, the user cannot hear anything. (2) Reminder: Play a sound file, if the phone is set to mute or vibrate, then through the vibration to remind the user. (3) Vibration: Vibrate the phone without considering other settings.
3, using the sample (first in class to introduce Audiotoolbox)
1 importAudioToolbox

(1) Sound playback

123456789101112 @IBAction func systemSound(sender: AnyObject) {    //建立的SystemSoundID对象    var soundID:SystemSoundID = 0    //获取声音地址    var path = NSBundle.mainBundle().pathForResource("msg", ofType: "wav")    //地址转换    var baseURL = NSURL(fileURLWithPath: path!)    //赋值    AudioServicesCreateSystemSoundID(baseURL, &soundID)    //播放声音    AudioServicesPlaySystemSound(soundID)}


(2) Reminders

123456789101112 @IBAction func systemAlert(sender: AnyObject) {    //建立的SystemSoundID对象    var soundID:SystemSoundID = 0    //获取声音地址    var path = NSBundle.mainBundle().pathForResource("msg", ofType: "wav")    //地址转换    var baseURL = NSURL(fileURLWithPath: path!)    //赋值    AudioServicesCreateSystemSoundID(baseURL, &soundID)    //提醒(同上面唯一的一个区别)    AudioServicesPlayAlertSound(soundID)}


(3) Vibration

123456 @IBAction func systemvibration (sender: anyobject      //established Systemsoundid object      var soundid = systemsoundid (ksystemsoundid_vibrate)       //vibration      audioservicesplaysystemsound (Soundid) }

Swift-use of the system sound service (play sounds, reminders, vibrations)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.