Skip to content

Instantly share code, notes, and snippets.

@erkanyildiz
Last active August 15, 2017 11:21
Show Gist options
  • 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
// 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
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment