Skip to content

Instantly share code, notes, and snippets.

@erkanyildiz
Last active August 15, 2017 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erkanyildiz/9db80c47a779f5b7b437 to your computer and use it in GitHub Desktop.
Save erkanyildiz/9db80c47a779f5b7b437 to your computer and use it in GitHub Desktop.
Audio manager with persistent muting
// erkanyildiz
// 20170815-2020+0900
//
// 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;
- (void)playSound:(NSString *)fileName;
@end
// erkanyildiz
// 20170815-2020+0900
//
// EYAudioManager.m
#import "EYAudioManager.h"
#define EYAMUDKey_isMuted @"EYAudioManager_isMuted"
@interface EYAudioManager()
@property (nonatomic, strong) AVAudioPlayer* backgroundTheme;
@property (nonatomic, readwrite) BOOL isMuted;
@property (nonatomic, strong) AVAudioPlayer* player;
@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;
self.player = [AVAudioPlayer.alloc initWithContentsOfURL:[NSURL fileURLWithPath:[NSBundle.mainBundle pathForResource:fileName ofType:nil]] error:NULL];
[self.player play];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment