Last active
August 15, 2017 11:21
Revisions
-
erkanyildiz revised this gist
Aug 15, 2017 . 2 changed files with 6 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ // erkanyildiz // 20170815-2020+0900 // // EYAudioManager.h This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ // erkanyildiz // 20170815-2020+0900 // // EYAudioManager.m @@ -8,9 +8,9 @@ @interface EYAudioManager() @property (nonatomic, strong) AVAudioPlayer* backgroundTheme; @property (nonatomic, readwrite) BOOL isMuted; @property (nonatomic, strong) AVAudioPlayer* player; @end @@ -93,11 +93,8 @@ - (void)playSound:(NSString *)fileName if (self.isMuted) return; self.player = [AVAudioPlayer.alloc initWithContentsOfURL:[NSURL fileURLWithPath:[NSBundle.mainBundle pathForResource:fileName ofType:nil]] error:NULL]; [self.player play]; } @end -
erkanyildiz revised this gist
Aug 14, 2017 . 2 changed files with 18 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ // erkanyildiz // 20170814-1518+0900 // // EYAudioManager.h @@ -11,10 +11,11 @@ extern NSString * const EYAudioManagerMuteStateChangedNotification; @property (nonatomic, readonly) BOOL isMuted; + (instancetype)sharedInstance; - (void)startBackgroundTheme:(NSString*)fileName; - (void)mute; - (void)unmute; - (void)toggleMute; - (void)playSound:(NSString *)fileName; @end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ // erkanyildiz // 20170814-1518+0900 // // EYAudioManager.m @@ -18,7 +18,7 @@ @implementation EYAudioManager NSString * const EYAudioManagerMuteStateChangedNotification = @"EYAudioManagerMuteStateChangedNotification"; + (instancetype)sharedInstance { static EYAudioManager * s_sharedInstance; static dispatch_once_t onceToken; @@ -27,7 +27,7 @@ +(instancetype)sharedInstance } - (id)init { self = [super init]; @@ -40,7 +40,7 @@ -(id)init } - (void)startBackgroundTheme:(NSString*)fileName { NSString* path = [NSBundle.mainBundle pathForResource:fileName.stringByDeletingPathExtension ofType:fileName.pathExtension]; if (!path) @@ -54,28 +54,28 @@ -(void)startBackgroundTheme:(NSString*)fileName } - (void)mute { self.isMuted = YES; [self saveStateAndPostNotification]; } - (void)unmute { self.isMuted = NO; [self saveStateAndPostNotification]; } - (void)toggleMute { self.isMuted = !self.isMuted; [self saveStateAndPostNotification]; } - (void)saveStateAndPostNotification { self.backgroundTheme.volume = self.isMuted?0.0:1.0; @@ -88,7 +88,7 @@ -(void)saveStateAndPostNotification } - (void)playSound:(NSString *)fileName { if (self.isMuted) return; @@ -100,4 +100,4 @@ -(void)playSound:(NSString *)fileName //TODO: mute already playing long duration audio files } @end -
erkanyildiz created this gist
Dec 8, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ // erkanyildiz // 20151208-1132 // // EYAudioManager.h #import <Foundation/Foundation.h> #import <AVFoundation/AVFoundation.h> extern NSString * const EYAudioManagerMuteStateChangedNotification; @interface EYAudioManager : NSObject <AVAudioPlayerDelegate> @property (nonatomic, readonly) BOOL isMuted; +(instancetype)sharedInstance; -(void)startBackgroundTheme:(NSString*)fileName; -(void)mute; -(void)unmute; -(void)toggleMute; @end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,103 @@ // erkanyildiz // 20151208-1132 // // EYAudioManager.m #import "EYAudioManager.h" #define EYAMUDKey_isMuted @"EYAudioManager_isMuted" @interface EYAudioManager() @property (nonatomic, strong) AVAudioPlayer *backgroundTheme; @property (nonatomic, readwrite) BOOL isMuted; @end @implementation EYAudioManager NSString * const EYAudioManagerMuteStateChangedNotification = @"EYAudioManagerMuteStateChangedNotification"; +(instancetype)sharedInstance { static EYAudioManager * s_sharedInstance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ s_sharedInstance = EYAudioManager.new;}); return s_sharedInstance; } -(id)init { self = [super init]; if(self) { self.isMuted = [NSUserDefaults.standardUserDefaults boolForKey:EYAMUDKey_isMuted]; } return self; } -(void)startBackgroundTheme:(NSString*)fileName { NSString* path = [NSBundle.mainBundle pathForResource:fileName.stringByDeletingPathExtension ofType:fileName.pathExtension]; if (!path) return; [self.backgroundTheme stop]; self.backgroundTheme = [AVAudioPlayer.alloc initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; self.backgroundTheme.numberOfLoops = -1; self.backgroundTheme.volume = self.isMuted?0.0:1.0; [self.backgroundTheme play]; } -(void)mute { self.isMuted = YES; [self saveStateAndPostNotification]; } -(void)unmute { self.isMuted = NO; [self saveStateAndPostNotification]; } -(void)toggleMute { self.isMuted = !self.isMuted; [self saveStateAndPostNotification]; } -(void)saveStateAndPostNotification { self.backgroundTheme.volume = self.isMuted?0.0:1.0; [NSNotificationCenter.defaultCenter postNotificationName:EYAudioManagerMuteStateChangedNotification object:@(self.isMuted) userInfo:nil]; [NSUserDefaults.standardUserDefaults setBool:self.isMuted forKey:EYAMUDKey_isMuted]; [NSUserDefaults.standardUserDefaults synchronize]; } -(void)playSound:(NSString *)fileName { if (self.isMuted) return; AVAudioPlayer * aSound = [AVAudioPlayer.alloc initWithContentsOfURL:[NSURL fileURLWithPath:[NSBundle.mainBundle pathForResource:fileName ofType:fileName.pathExtension]] error:NULL]; [aSound play]; //TODO: add completion block //TODO: mute already playing long duration audio files } @end