|
| 1 | +// |
| 2 | +// SoundEffect.swift |
| 3 | +// Sample class for quickly incorporating sound effects. |
| 4 | +// |
| 5 | +// Created by Mark Hamilton on 2/16/16. |
| 6 | +// Copyright © 2016 dryverless. (http://www.dryverless.com) |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in all |
| 16 | +// copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +// SOFTWARE. |
| 25 | +// |
| 26 | + |
| 27 | +import UIKit |
| 28 | +import AVFoundation |
| 29 | + |
| 30 | +class SoundEffect { |
| 31 | + |
| 32 | + private var _name: String! = "sound" |
| 33 | + private var _ofType: String! = "wav" |
| 34 | + private var _isEnabled: Bool! = true |
| 35 | + private var _loop: Bool! = false |
| 36 | + private var _playing: Bool! = false |
| 37 | + private var _volume: Float! = 1.0 |
| 38 | + private var _duration: TimeInterval! = nil |
| 39 | + private var _numberOfLoops: Int! = 0 |
| 40 | + |
| 41 | + var sound: AVAudioPlayer! |
| 42 | + |
| 43 | + var name: String { |
| 44 | + get { |
| 45 | + return _name |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + var ofType: String { |
| 50 | + get { |
| 51 | + return _ofType |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + var isEnabled: Bool { |
| 56 | + get { |
| 57 | + if let enableBool: Bool = _isEnabled { |
| 58 | + return enableBool |
| 59 | + } else { |
| 60 | + return false |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + var loop: Bool { |
| 66 | + get { |
| 67 | + return _loop |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + var playing: Bool { |
| 72 | + get { |
| 73 | + if let isPlaying: Bool = sound.isPlaying { |
| 74 | + return isPlaying |
| 75 | + } |
| 76 | + else { |
| 77 | + return _playing |
| 78 | + // return false |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + var volume: Float { |
| 84 | + get { |
| 85 | + return _volume |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + var duration: TimeInterval { |
| 90 | + get { |
| 91 | + |
| 92 | + guard let soundDuration: TimeInterval = sound.duration as TimeInterval else { |
| 93 | + return _duration |
| 94 | + } |
| 95 | + |
| 96 | + return soundDuration |
| 97 | + |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + var numberOfLoops: Int { |
| 102 | + get { |
| 103 | + return _numberOfLoops |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + init(fileName: String, fileType: String) { |
| 108 | + |
| 109 | + self._name = fileName |
| 110 | + self._ofType = fileType |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + init(fileName: String, fileType: String, enableSound: Bool?, enableLooping: Bool?) { |
| 115 | + |
| 116 | + self._name = fileName |
| 117 | + self._ofType = fileType |
| 118 | + |
| 119 | + if let setEnable = enableSound { |
| 120 | + self._isEnabled = setEnable |
| 121 | + } |
| 122 | + |
| 123 | + if let looping = enableLooping { |
| 124 | + self._loop = looping |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + init(fileName: String, fileType: String, enableSound: Bool?, enableLooping: Bool?, loopTotal: Int?, defaultVolume: Float?) { |
| 130 | + |
| 131 | + self._name = fileName |
| 132 | + self._ofType = fileType |
| 133 | + |
| 134 | + if let setEnable = enableSound { |
| 135 | + self._isEnabled = setEnable |
| 136 | + } |
| 137 | + |
| 138 | + if let looping = enableLooping { |
| 139 | + self._loop = looping |
| 140 | + } |
| 141 | + |
| 142 | + if let loopCount = loopTotal { |
| 143 | + self._numberOfLoops = loopCount |
| 144 | + } |
| 145 | + |
| 146 | + if let setVolume = defaultVolume { |
| 147 | + self._volume = setVolume |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + init(fileName: String, fileType: String, enableSound: Bool?, enableLooping: Bool?, defaultVolume: Float?) { |
| 152 | + |
| 153 | + self._name = fileName |
| 154 | + self._ofType = fileType |
| 155 | + |
| 156 | + if let setEnable = enableSound { |
| 157 | + self._isEnabled = setEnable |
| 158 | + } |
| 159 | + |
| 160 | + if let looping = enableLooping { |
| 161 | + self._loop = looping |
| 162 | + } |
| 163 | + |
| 164 | + if let setVolume = defaultVolume { |
| 165 | + self._volume = setVolume |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + init() { |
| 170 | + // must have sound.wav file |
| 171 | + } |
| 172 | + |
| 173 | + func prepareToPlay() { |
| 174 | + |
| 175 | + let path = Bundle.main().pathForResource(self.name, ofType: self.ofType) |
| 176 | + |
| 177 | + let soundUrl = URL(fileURLWithPath: path!) |
| 178 | + |
| 179 | + do { |
| 180 | + |
| 181 | + try sound = AVAudioPlayer(contentsOf: soundUrl) |
| 182 | + |
| 183 | + if loop { |
| 184 | + |
| 185 | + // infinite looping |
| 186 | + sound.numberOfLoops = -1 |
| 187 | + } |
| 188 | + |
| 189 | + if volume != 1.0 { |
| 190 | + |
| 191 | + // set to non-default volume level |
| 192 | + sound.volume = volume |
| 193 | + } |
| 194 | + |
| 195 | + sound.prepareToPlay() |
| 196 | + |
| 197 | + } catch let err as NSError { |
| 198 | + |
| 199 | + print(err.debugDescription) |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + func play() { |
| 204 | + |
| 205 | + if sound.isPlaying { |
| 206 | + sound.stop() |
| 207 | + } |
| 208 | + |
| 209 | + if isEnabled == true { |
| 210 | + sound.play() |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + func stop() { |
| 215 | + |
| 216 | + if sound.isPlaying { |
| 217 | + sound.stop() |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + func pause() { |
| 222 | + |
| 223 | + if sound.isPlaying { |
| 224 | + sound.pause() |
| 225 | + } |
| 226 | + |
| 227 | + } |
| 228 | + |
| 229 | + func enable() { |
| 230 | + |
| 231 | + _isEnabled = true |
| 232 | + } |
| 233 | + |
| 234 | + func disable() { |
| 235 | + |
| 236 | + _isEnabled = false |
| 237 | + } |
| 238 | + |
| 239 | + func toggle() { |
| 240 | + |
| 241 | + _isEnabled = !_isEnabled |
| 242 | + } |
| 243 | + |
| 244 | + func setVolume(level: Float) { |
| 245 | + |
| 246 | + if sound.isPlaying { |
| 247 | + sound.volume = level |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | +} |
0 commit comments