2018-08-14 11:53:02 +00:00
|
|
|
#import "Sounds.h"
|
2018-08-13 16:29:26 +00:00
|
|
|
|
2018-08-14 11:53:02 +00:00
|
|
|
@interface Sounds ()
|
2018-08-13 16:29:26 +00:00
|
|
|
|
|
|
|
@property (nonatomic, strong) AVAudioPlayer* backgroundPlayer;
|
|
|
|
|
|
|
|
@property (nonatomic, strong) AVAudioPlayer* gunshotPlayer;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2018-08-14 11:53:02 +00:00
|
|
|
@implementation Sounds
|
2018-08-13 16:29:26 +00:00
|
|
|
|
2018-08-14 11:53:02 +00:00
|
|
|
static Sounds *_shareInstance;
|
2018-08-13 16:29:26 +00:00
|
|
|
|
|
|
|
+(instancetype)shareInstance {
|
|
|
|
_shareInstance = [[super allocWithZone:NULL] init];
|
|
|
|
return _shareInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)playBackgroundSound {
|
|
|
|
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"background_sound" ofType:@"mp3"];
|
|
|
|
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
|
|
|
|
self.backgroundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
|
|
|
|
self.backgroundPlayer.numberOfLoops = -1; //Infinite
|
|
|
|
self.backgroundPlayer.play;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopBackgroundSound {
|
|
|
|
if (self.backgroundPlayer != nil && self.backgroundPlayer.isPlaying) {
|
|
|
|
self.backgroundPlayer.stop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)playGunshotSound {
|
|
|
|
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"gunshot_sound" ofType:@"mp3"];
|
|
|
|
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
|
|
|
|
self.gunshotPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
|
|
|
|
self.gunshotPlayer.numberOfLoops = 0;
|
|
|
|
self.gunshotPlayer.play;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopGunshotSound {
|
|
|
|
if (self.gunshotPlayer != nil && self.gunshotPlayer.isPlaying) {
|
|
|
|
self.gunshotPlayer.stop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|