-
Notifications
You must be signed in to change notification settings - Fork 3
/
NUAudio.m
71 lines (55 loc) · 1.72 KB
/
NUAudio.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
64
65
66
67
68
69
70
71
//
// NUAudio.m
// util
//
// Created by mrnuku on 2015. 02. 21..
// Copyright (c) 2015. scientefic_station2. All rights reserved.
//
#import "NUAudio.h"
@implementation NUAudioFile
- (void)playAudioFile {
AudioServicesPlaySystemSound(self.soundId);
}
@end
@implementation NUAudioManager {
NSMutableDictionary *_audioFilesDict;
}
- (instancetype)init {
self = [super init];
if (self) {
_audioFilesDict = [NSMutableDictionary new];
}
return self;
}
- (void)dealloc {
[_audioFilesDict enumerateKeysAndObjectsUsingBlock:^(NSString *fileName, NUAudioFile *audioFile, BOOL *stop) {
if(audioFile.soundId != kAudioLoader_DefaultId) {
AudioServicesDisposeSystemSoundID(audioFile.soundId);
}
}];
}
- (NUAudioFile *)loadAudioFile:(NSString *)fileName {
NUAudioFile *audioFile = [_audioFilesDict objectForKey:fileName];
if(!audioFile) {
NSString *pathExt = fileName.pathExtension;
NSString *pathWithoutExt = [fileName stringByDeletingPathExtension];
NSString *path = [[NSBundle mainBundle] pathForResource:pathWithoutExt ofType:pathExt];
SystemSoundID soundId;
if(path.length) {
NSURL* url = [NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundId);
}
else {
soundId = kAudioLoader_DefaultId;
}
audioFile = [NUAudioFile new];
audioFile.soundId = soundId;
[_audioFilesDict setObject:audioFile forKey:fileName];
}
return audioFile;
}
- (void)playAudioFile:(NSString *)fileName {
NUAudioFile *audioFile = [self loadAudioFile:fileName];
[audioFile playAudioFile];
}
@end