-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundManager.m
63 lines (54 loc) · 1.35 KB
/
SoundManager.m
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// SoundManager.m
// SoundMan
//
// Created by Josh Levine on 2/8/16.
// Copyright © 2016 Josh Levine. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>
#import "SoundManager.h"
@implementation SoundManager
{
AVAudioPlayer *player;
SoundPlayedCompletion completionBlock;
}
+ (SoundManager*)sharedInstance {
static SoundManager *_sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[SoundManager alloc] init];
});
return _sharedInstance;
}
- (void)playSoundFromURL:(NSURL*)url completion:(SoundPlayedCompletion)completion {
if (player != nil) {
[player stop];
player = nil;
}
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error != nil) {
completion(NO);
return;
}
completionBlock = completion;
player.delegate = self;
player.volume = 1.0;
[player prepareToPlay];
[player play];
}
- (void)stopSound {
if (player != nil) {
[player stop];
player = nil;
}
}
#pragma mark - AVAudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
self->player = nil;
if (completionBlock != nil) {
completionBlock(flag);
completionBlock = nil;
}
}
@end