Skip to content

Instantly share code, notes, and snippets.

@erkanyildiz
Last active August 15, 2017 11:21

Revisions

  1. erkanyildiz revised this gist Aug 15, 2017. 2 changed files with 6 additions and 9 deletions.
    2 changes: 1 addition & 1 deletion EYAudioManager.h
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // erkanyildiz
    // 20170814-1518+0900
    // 20170815-2020+0900
    //
    // EYAudioManager.h

    13 changes: 5 additions & 8 deletions EYAudioManager.m
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // erkanyildiz
    // 20170814-1518+0900
    // 20170815-2020+0900
    //
    // EYAudioManager.m

    @@ -8,9 +8,9 @@

    @interface EYAudioManager()

    @property (nonatomic, strong) AVAudioPlayer *backgroundTheme;
    @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;

    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
    self.player = [AVAudioPlayer.alloc initWithContentsOfURL:[NSURL fileURLWithPath:[NSBundle.mainBundle pathForResource:fileName ofType:nil]] error:NULL];
    [self.player play];
    }

    @end
  2. erkanyildiz revised this gist Aug 14, 2017. 2 changed files with 18 additions and 17 deletions.
    15 changes: 8 additions & 7 deletions EYAudioManager.h
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // erkanyildiz
    // 20151208-1132
    // 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;
    + (instancetype)sharedInstance;
    - (void)startBackgroundTheme:(NSString*)fileName;
    - (void)mute;
    - (void)unmute;
    - (void)toggleMute;
    - (void)playSound:(NSString *)fileName;

    @end
    @end
    20 changes: 10 additions & 10 deletions EYAudioManager.m
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // erkanyildiz
    // 20151208-1132
    // 20170814-1518+0900
    //
    // EYAudioManager.m

    @@ -18,7 +18,7 @@ @implementation EYAudioManager

    NSString * const EYAudioManagerMuteStateChangedNotification = @"EYAudioManagerMuteStateChangedNotification";

    +(instancetype)sharedInstance
    + (instancetype)sharedInstance
    {
    static EYAudioManager * s_sharedInstance;
    static dispatch_once_t onceToken;
    @@ -27,7 +27,7 @@ +(instancetype)sharedInstance
    }


    -(id)init
    - (id)init
    {
    self = [super init];

    @@ -40,7 +40,7 @@ -(id)init
    }


    -(void)startBackgroundTheme:(NSString*)fileName
    - (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
    - (void)mute
    {
    self.isMuted = YES;
    [self saveStateAndPostNotification];
    }


    -(void)unmute
    - (void)unmute
    {
    self.isMuted = NO;
    [self saveStateAndPostNotification];
    }


    -(void)toggleMute
    - (void)toggleMute
    {
    self.isMuted = !self.isMuted;
    [self saveStateAndPostNotification];
    }


    -(void)saveStateAndPostNotification
    - (void)saveStateAndPostNotification
    {
    self.backgroundTheme.volume = self.isMuted?0.0:1.0;

    @@ -88,7 +88,7 @@ -(void)saveStateAndPostNotification
    }


    -(void)playSound:(NSString *)fileName
    - (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
    @end
  3. erkanyildiz created this gist Dec 8, 2015.
    20 changes: 20 additions & 0 deletions EYAudioManager.h
    Original 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
    103 changes: 103 additions & 0 deletions EYAudioManager.m
    Original 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