Skip to content

Commit 23fc07d

Browse files
author
Diogo Autilio
committed
Add play sound capability
Update Success example
1 parent 3a5b500 commit 23fc07d

5 files changed

Lines changed: 45 additions & 1 deletion

File tree

SCLAlertView.xcodeproj/project.pbxproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
DD4BA9BE19DED822008D73EB /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD4BA9BD19DED822008D73EB /* AVFoundation.framework */; };
11+
DD4BA9C119DED8EF008D73EB /* right_answer.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = DD4BA9C019DED8EF008D73EB /* right_answer.mp3 */; };
1012
DD7282B919D6087C00077F54 /* Storyboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DD7282B719D6087C00077F54 /* Storyboard.storyboard */; };
1113
DDB15FBD19D5B7C600173158 /* SCLAlertViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DDB15FBC19D5B7C600173158 /* SCLAlertViewTests.m */; };
1214
DDB15FCA19D5B88A00173158 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = DDB15FC719D5B88A00173158 /* AppDelegate.m */; };
@@ -31,6 +33,8 @@
3133

3234
/* Begin PBXFileReference section */
3335
DD0D295B19D902DA00881F53 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = SCLAlertViewExample/Info.plist; sourceTree = SOURCE_ROOT; };
36+
DD4BA9BD19DED822008D73EB /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
37+
DD4BA9C019DED8EF008D73EB /* right_answer.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = right_answer.mp3; sourceTree = "<group>"; };
3438
DD7282B819D6087C00077F54 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = SCLAlertViewExample/Base.lproj/Storyboard.storyboard; sourceTree = SOURCE_ROOT; };
3539
DDB15F9D19D5B7C600173158 /* SCLAlertView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SCLAlertView.app; sourceTree = BUILT_PRODUCTS_DIR; };
3640
DDB15FB619D5B7C600173158 /* SCLAlertViewTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SCLAlertViewTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -57,6 +61,7 @@
5761
isa = PBXFrameworksBuildPhase;
5862
buildActionMask = 2147483647;
5963
files = (
64+
DD4BA9BE19DED822008D73EB /* AVFoundation.framework in Frameworks */,
6065
);
6166
runOnlyForDeploymentPostprocessing = 0;
6267
};
@@ -70,9 +75,19 @@
7075
/* End PBXFrameworksBuildPhase section */
7176

7277
/* Begin PBXGroup section */
78+
DD4BA9BF19DED8EF008D73EB /* Sounds */ = {
79+
isa = PBXGroup;
80+
children = (
81+
DD4BA9C019DED8EF008D73EB /* right_answer.mp3 */,
82+
);
83+
name = Sounds;
84+
path = SCLAlertViewExample/Sounds;
85+
sourceTree = SOURCE_ROOT;
86+
};
7387
DDB15F9419D5B7C600173158 = {
7488
isa = PBXGroup;
7589
children = (
90+
DD4BA9BD19DED822008D73EB /* AVFoundation.framework */,
7691
DDB15FD519D5B96500173158 /* SCLAlertView */,
7792
DDB15F9F19D5B7C600173158 /* SCLAlertViewExample */,
7893
DDB15FB919D5B7C600173158 /* SCLAlertViewTests */,
@@ -107,6 +122,7 @@
107122
DDB15FA019D5B7C600173158 /* Supporting Files */ = {
108123
isa = PBXGroup;
109124
children = (
125+
DD4BA9BF19DED8EF008D73EB /* Sounds */,
110126
DD0D295B19D902DA00881F53 /* Info.plist */,
111127
DDB15FD319D5B8DB00173158 /* main.m */,
112128
);
@@ -225,6 +241,7 @@
225241
isa = PBXResourcesBuildPhase;
226242
buildActionMask = 2147483647;
227243
files = (
244+
DD4BA9C119DED8EF008D73EB /* right_answer.mp3 in Resources */,
228245
DDB15FD019D5B8BF00173158 /* Images.xcassets in Resources */,
229246
DD7282B919D6087C00077F54 /* Storyboard.storyboard in Resources */,
230247
);

SCLAlertView/SCLAlertView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ typedef NS_ENUM(NSInteger, SCLAlertViewStyle)
2525

2626
@property UILabel *labelTitle;
2727
@property UITextView *viewText;
28-
28+
@property (nonatomic, strong) NSURL *soundURL;
2929

3030
- (void)hideView;
3131

SCLAlertView/SCLAlertView.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "SCLButton.h"
1111
#import "SCLAlertViewResponder.h"
1212
#import "SCLAlertViewStyleKit.h"
13+
#import <AVFoundation/AVFoundation.h>
1314

1415
#define UIColorFromRGB(rgbValue) [UIColor \
1516
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
@@ -26,6 +27,7 @@ @interface SCLAlertView ()
2627
@property (nonatomic, strong) UIView *shadowView;
2728
@property (nonatomic, strong) UIView *contentView;
2829
@property (nonatomic, strong) UIView *circleViewBackground;
30+
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
2931

3032
@end
3133

@@ -192,6 +194,16 @@ -(void)viewWillLayoutSubviews
192194
}
193195
}
194196

197+
#pragma mark --
198+
#pragma mark Sound
199+
200+
- (void)setSoundURL:(NSURL *)soundURL
201+
{
202+
NSError *error;
203+
_soundURL = soundURL;
204+
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:_soundURL error:&error];
205+
}
206+
195207
#pragma mark --
196208
#pragma mark TextField
197209

@@ -356,6 +368,19 @@ -(SCLAlertViewResponder *)showTitle:(UIViewController *)vc title:(NSString *)tit
356368
kTextHeight = ht;
357369
}
358370
}
371+
372+
// Play sound, if necessary
373+
if(_soundURL != nil)
374+
{
375+
if (_audioPlayer == nil)
376+
{
377+
NSLog(@"You need to set your sound file first");
378+
}
379+
else
380+
{
381+
[_audioPlayer play];
382+
}
383+
}
359384

360385
// Add button, if necessary
361386
if(completeText != nil)
19.6 KB
Binary file not shown.

SCLAlertViewExample/ViewController.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ - (IBAction)showSuccess:(id)sender
4242
[alert addButton:@"Second Button" actionBlock:^(void) {
4343
NSLog(@"Second button tapped");
4444
}];
45+
46+
alert.soundURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/right_answer.mp3", [[NSBundle mainBundle] resourcePath]]];
4547

4648
[alert showSuccess:self title:kSuccessTitle subTitle:kSubtitle closeButtonTitle:kButtonTitle duration:0.0f];
4749
}

0 commit comments

Comments
 (0)