Skip to content

Commit c06a95c

Browse files
committed
Create SoundEffect.swift
1 parent 53c6c50 commit c06a95c

File tree

1 file changed

+249
-0
lines changed

1 file changed

+249
-0
lines changed

SoundEffect.swift

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
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: NSTimeInterval! = 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.playing {
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: NSTimeInterval {
90+
get {
91+
if let soundDuration: NSTimeInterval = sound.duration {
92+
return soundDuration
93+
} else {
94+
return _duration
95+
}
96+
}
97+
}
98+
99+
var numberOfLoops: Int {
100+
get {
101+
return _numberOfLoops
102+
}
103+
}
104+
105+
init(fileName: String, fileType: String) {
106+
107+
self._name = fileName
108+
self._ofType = fileType
109+
110+
}
111+
112+
init(fileName: String, fileType: String, enableSound: Bool?, enableLooping: Bool?) {
113+
114+
self._name = fileName
115+
self._ofType = fileType
116+
117+
if let setEnable = enableSound {
118+
self._isEnabled = setEnable
119+
}
120+
121+
if let looping = enableLooping {
122+
self._loop = looping
123+
}
124+
125+
}
126+
127+
init(fileName: String, fileType: String, enableSound: Bool?, enableLooping: Bool?, loopTotal: Int?, defaultVolume: Float?) {
128+
129+
self._name = fileName
130+
self._ofType = fileType
131+
132+
if let setEnable = enableSound {
133+
self._isEnabled = setEnable
134+
}
135+
136+
if let looping = enableLooping {
137+
self._loop = looping
138+
}
139+
140+
if let loopCount = loopTotal {
141+
self._numberOfLoops = loopCount
142+
}
143+
144+
if let setVolume = defaultVolume {
145+
self._volume = setVolume
146+
}
147+
}
148+
149+
init(fileName: String, fileType: String, enableSound: Bool?, enableLooping: Bool?, defaultVolume: Float?) {
150+
151+
self._name = fileName
152+
self._ofType = fileType
153+
154+
if let setEnable = enableSound {
155+
self._isEnabled = setEnable
156+
}
157+
158+
if let looping = enableLooping {
159+
self._loop = looping
160+
}
161+
162+
if let setVolume = defaultVolume {
163+
self._volume = setVolume
164+
}
165+
}
166+
167+
init() {
168+
// must have sound.wav file
169+
}
170+
171+
func prepareToPlay() {
172+
173+
let path = NSBundle.mainBundle().pathForResource(self.name, ofType: self.ofType)
174+
175+
let soundUrl = NSURL(fileURLWithPath: path!)
176+
177+
do {
178+
179+
try sound = AVAudioPlayer(contentsOfURL: soundUrl)
180+
181+
if loop {
182+
183+
// infinite looping
184+
sound.numberOfLoops = -1
185+
}
186+
187+
if volume != 1.0 {
188+
189+
// set to non-default volume level
190+
sound.volume = volume
191+
}
192+
193+
sound.prepareToPlay()
194+
195+
} catch let err as NSError {
196+
197+
print(err.debugDescription)
198+
}
199+
}
200+
201+
func play() {
202+
203+
if sound.playing {
204+
sound.stop()
205+
}
206+
207+
if isEnabled == true {
208+
sound.play()
209+
}
210+
}
211+
212+
func stop() {
213+
214+
if sound.playing {
215+
sound.stop()
216+
}
217+
}
218+
219+
func pause() {
220+
221+
if sound.playing {
222+
sound.pause()
223+
}
224+
225+
}
226+
227+
func enable() {
228+
229+
_isEnabled = true
230+
}
231+
232+
func disable() {
233+
234+
_isEnabled = false
235+
}
236+
237+
func toggle() {
238+
239+
_isEnabled = !_isEnabled
240+
}
241+
242+
func setVolume(level: Float) {
243+
244+
if sound.playing {
245+
sound.volume = level
246+
}
247+
}
248+
249+
}

0 commit comments

Comments
 (0)