-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioManager.swift
More file actions
48 lines (37 loc) · 1.02 KB
/
Copy pathAudioManager.swift
File metadata and controls
48 lines (37 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// AudioManager.swift
// jackTheGiant
//
// Created by Leon on 14/12/16.
// Copyright © 2016 Leon. All rights reserved.
//
import AVFoundation
class AudioManager {
// Singleton
static let instance = AudioManager();
private init() {}
private var audioPlayer: AVAudioPlayer?;
func playBGMusic() {
let url = Bundle.main.url(forResource: "Background music", withExtension: "mp3");
var err: NSError?;
do {
try audioPlayer = AVAudioPlayer(contentsOf: url!);
audioPlayer?.numberOfLoops = -1;
audioPlayer?.prepareToPlay();
audioPlayer?.play();
} catch let err1 as NSError {
err = err1;
}
if err != nil {
print(err?.description as Any);
}
}
func stopBGMusic() {
if (audioPlayer?.isPlaying) != nil {
audioPlayer?.stop();
}
}
func isAudioPlayerInitialized() -> Bool {
return audioPlayer == nil;
}
}