forked from pcperini-historic/UISound
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUISound.m
More file actions
128 lines (106 loc) · 3.79 KB
/
UISound.m
File metadata and controls
128 lines (106 loc) · 3.79 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// UISound.m
//
// Created by Patrick Perini on 11/29/11.
// Licensing information available in README.md
//
#import "UISound.h"
#pragma mark UISound Private Interface
//========================================================================//
void systemAudioCallback(SystemSoundID soundID, void *clientData);
//========================================================================//
#pragma mark UISound Public Implementation
//========================================================================//
@implementation UISound
@synthesize name;
@synthesize delegate;
- (id)initWithName: (NSString *)soundName
{
self = [super init];
if (self)
{
name = [soundName copy];
NSString *path;
for (NSString *ext in [UISound soundUnfilteredTypes])
{
path = [[NSBundle mainBundle] pathForResource: soundName
ofType: ext];
if ([[NSFileManager defaultManager] fileExistsAtPath: path])
{
NSURL *pathURL = [NSURL fileURLWithPath: path];
AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef) pathURL, &audioEffect);
AudioServicesAddSystemSoundCompletion(audioEffect,
NULL,
NULL,
systemAudioCallback,
(__bridge_retained void *) self);
return self;
}
}
}
return nil;
}
- (id)initWithContentsOfURL:(NSURL *)url
{
self = [super init];
if (self)
{
name = [url lastPathComponent];
AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef) url, &audioEffect);
AudioServicesAddSystemSoundCompletion(audioEffect,
NULL,
NULL,
systemAudioCallback,
(__bridge_retained void *) self);
return self;
}
return nil;
}
+ (id)soundNamed: (NSString *)soundName
{return [[UISound alloc] initWithName: soundName];}
+ (id)soundWithContentsOfURL:(NSURL *)url
{return [[UISound alloc] initWithContentsOfURL: url];}
+ (NSArray *)soundUnfilteredTypes
{
return [NSArray arrayWithObjects: @"aac",
@"aiff",
@"wav",
@"wma",
@"mp3",
@"m4a", nil];
}
- (void)play
{AudioServicesPlaySystemSound(audioEffect);}
- (void)alert
{AudioServicesPlayAlertSound(audioEffect);}
- (void)playWithCompletion:(void (^)(BOOL finished))completion {
self.completionBlock = completion;
[self play];
}
- (void)alertWithCompletion:(void (^)(BOOL finished))completion {
self.completionBlock = completion;
[self alert];
}
- (void)dealloc
{
AudioServicesRemoveSystemSoundCompletion(audioEffect);
AudioServicesDisposeSystemSoundID(audioEffect);
}
@end
//========================================================================//
#pragma mark UISound Private Implementation
//========================================================================//
void systemAudioCallback(SystemSoundID soundID, void *clientData)
{
UISound *sound = (__bridge_transfer UISound *) clientData;
if (sound.delegate)
{
[sound.delegate sound: sound
didFinishPlaying: YES];
}
if (sound.completionBlock)
{
sound.completionBlock(YES);
}
}
//========================================================================//