Created
February 12, 2016 17:45
-
-
Save mingsai/93cdb3692493a4bcd1d0 to your computer and use it in GitHub Desktop.
A Swift class to manage initialization, playback and memory related to sound playback on iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MNGSoundManager.swift | |
// | |
// | |
// Created by Tommie N. Carter, Jr., MBA on 10/26/15. | |
// Copyright © 2015 MING Technology. All rights reserved. | |
// | |
import Foundation | |
import AudioToolbox | |
class MNGSoundManager { | |
static let sharedInstance = MNGSoundManager() | |
//urls for sound clips | |
let backSoundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("264447__kickhat__open-button-2", ofType: "wav")!) | |
let buttonSoundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("80921__justinbw__buttonchime02up", ofType: "wav")!) | |
let deleteSoundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("67454__splashdust__negativebeep", ofType: "wav")!) | |
let dropSoundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("191678__porphyr__waterdrop", ofType: "wav")!) | |
let mainSwitchOpenSoundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("195912__acpascal__start-beep", ofType: "wav")!) | |
let mainSwitchCloseSoundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("191754__fins__button-5", ofType: "wav")!) | |
let popupSoundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("242502__gabrielaraujo__pop-up-notification", ofType: "wav")!) | |
let optionSoundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("126418__cabeeno-rossley__button-select", ofType: "wav")!) | |
private var _backSnd:SystemSoundID? = nil | |
lazy var backSound: SystemSoundID = { | |
if self._backSnd != nil { return self._backSnd! } | |
var sndID : SystemSoundID = 0 | |
let url = self.backSoundURL | |
AudioServicesCreateSystemSoundID ( | |
url, | |
&sndID) | |
self._backSnd = sndID | |
return sndID | |
}() | |
private var _btnSnd:SystemSoundID? = nil | |
lazy var buttonSound: SystemSoundID = { | |
if self._btnSnd != nil { return self._btnSnd! } | |
var sndID : SystemSoundID = 0 | |
let url = self.buttonSoundURL | |
AudioServicesCreateSystemSoundID ( | |
url, | |
&sndID) | |
self._btnSnd = sndID | |
return sndID | |
}() | |
private var _deleteSnd:SystemSoundID? = nil | |
lazy var deleteSound: SystemSoundID = { | |
if self._deleteSnd != nil { return self._deleteSnd! } | |
var sndID : SystemSoundID = 0 | |
let url = self.deleteSoundURL | |
AudioServicesCreateSystemSoundID ( | |
url, | |
&sndID) | |
self._deleteSnd = sndID | |
return sndID | |
}() | |
private var _dropSnd:SystemSoundID? = nil | |
lazy var dropSound: SystemSoundID = { | |
if self._dropSnd != nil { return self._dropSnd! } | |
var sndID : SystemSoundID = 0 | |
let url = self.dropSoundURL | |
AudioServicesCreateSystemSoundID ( | |
url, | |
&sndID) | |
self._dropSnd = sndID | |
return sndID | |
}() | |
private var _mainSndClose:SystemSoundID? = nil | |
lazy var mainSwitchCloseSound: SystemSoundID = { | |
if self._mainSndClose != nil { return self._mainSndClose! } | |
var sndID : SystemSoundID = 0 | |
let url = self.mainSwitchCloseSoundURL | |
AudioServicesCreateSystemSoundID ( | |
url, | |
&sndID) | |
self._mainSndClose = sndID | |
return sndID | |
}() | |
private var _mainSndOpen:SystemSoundID? = nil | |
lazy var mainSwitchOpenSound: SystemSoundID = { | |
if self._mainSndOpen != nil { return self._mainSndOpen! } | |
var sndID : SystemSoundID = 0 | |
let url = self.mainSwitchOpenSoundURL | |
AudioServicesCreateSystemSoundID ( | |
url, | |
&sndID) | |
self._mainSndOpen = sndID | |
return sndID | |
}() | |
private var _popupSnd:SystemSoundID? = nil | |
lazy var popupSound: SystemSoundID = { | |
if self._popupSnd != nil { return self._popupSnd! } | |
var sndID : SystemSoundID = 0 | |
let url = self.popupSoundURL | |
AudioServicesCreateSystemSoundID ( | |
url, | |
&sndID) | |
self._popupSnd = sndID | |
return sndID | |
}() | |
private var _optionSnd:SystemSoundID? = nil | |
lazy var optionSound: SystemSoundID = { | |
if self._optionSnd != nil { return self._optionSnd! } | |
var sndID : SystemSoundID = 0 | |
let url = self.optionSoundURL | |
AudioServicesCreateSystemSoundID ( | |
url, | |
&sndID) | |
self._optionSnd = sndID | |
return sndID | |
}() | |
deinit | |
{ | |
AudioServicesDisposeSystemSoundID(self.buttonSound) | |
AudioServicesDisposeSystemSoundID(self.mainSwitchOpenSound) | |
AudioServicesDisposeSystemSoundID(self.mainSwitchCloseSound) | |
AudioServicesDisposeSystemSoundID(self.deleteSound) | |
AudioServicesDisposeSystemSoundID(self.dropSound) | |
AudioServicesDisposeSystemSoundID(self.optionSound) | |
AudioServicesDisposeSystemSoundID(self.popupSound) | |
_backSnd = nil | |
_deleteSnd = nil | |
_dropSnd = nil | |
_mainSndClose = nil | |
_mainSndOpen = nil | |
_optionSnd = nil | |
_popupSnd = nil | |
} | |
func playBackSound(){ | |
AudioServicesPlaySystemSound(self.backSound) | |
} | |
func playButtonSound(){ | |
AudioServicesPlaySystemSound(self.buttonSound) | |
} | |
func playDeleteSound(){ | |
AudioServicesPlaySystemSound(self.deleteSound) | |
} | |
func playDropSound(){ | |
AudioServicesPlaySystemSound(self.dropSound) | |
} | |
func playMainSwitchOpenSound(){ | |
AudioServicesPlaySystemSound(self.mainSwitchOpenSound) | |
} | |
func playMainSwitchCloseSound(){ | |
AudioServicesPlaySystemSound(self.mainSwitchCloseSound) | |
} | |
func playPopupSound(){ | |
AudioServicesPlaySystemSound(self.popupSound) | |
} | |
func playOptionSound(){ | |
AudioServicesPlaySystemSound(self.optionSound) | |
} | |
func vibrateDevice() { | |
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment