From 584b48c3abbf5f5c60fe2df6a2526076575371c9 Mon Sep 17 00:00:00 2001 From: Artem Budarin Date: Mon, 13 Aug 2018 21:29:26 +0500 Subject: [PATCH] added sound players from obj-c --- .../project.pbxproj | 6 +++ proj.ios/SoundCalls.h | 16 ++++++ proj.ios/SoundCalls.m | 49 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 proj.ios/SoundCalls.h create mode 100644 proj.ios/SoundCalls.m diff --git a/proj.ios/Double Hit Balls.xcodeproj/project.pbxproj b/proj.ios/Double Hit Balls.xcodeproj/project.pbxproj index 0512f6f..75e0fdb 100755 --- a/proj.ios/Double Hit Balls.xcodeproj/project.pbxproj +++ b/proj.ios/Double Hit Balls.xcodeproj/project.pbxproj @@ -36,6 +36,7 @@ AC67C87F211C8DE3003AA164 /* NativeSoundCallsImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC67C87E211C8DE3003AA164 /* NativeSoundCallsImpl.swift */; }; AC67C882211C8E24003AA164 /* gunshot_sound.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = AC67C880211C8E24003AA164 /* gunshot_sound.mp3 */; }; AC67C883211C8E24003AA164 /* background_sound.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = AC67C881211C8E24003AA164 /* background_sound.mp3 */; }; + ACC6F8382121E8DF00CEDC5C /* SoundCalls.m in Sources */ = {isa = PBXBuildFile; fileRef = ACC6F8372121E8DF00CEDC5C /* SoundCalls.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -111,6 +112,8 @@ AC67C87E211C8DE3003AA164 /* NativeSoundCallsImpl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeSoundCallsImpl.swift; sourceTree = ""; }; AC67C880211C8E24003AA164 /* gunshot_sound.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = gunshot_sound.mp3; sourceTree = ""; }; AC67C881211C8E24003AA164 /* background_sound.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = background_sound.mp3; sourceTree = ""; }; + ACC6F8372121E8DF00CEDC5C /* SoundCalls.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SoundCalls.m; sourceTree = ""; }; + ACC6F8392121E8ED00CEDC5C /* SoundCalls.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SoundCalls.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -256,6 +259,8 @@ children = ( AC67C87C211C8DD3003AA164 /* NativeSoundCalls.swift */, AC67C87E211C8DE3003AA164 /* NativeSoundCallsImpl.swift */, + ACC6F8372121E8DF00CEDC5C /* SoundCalls.m */, + ACC6F8392121E8ED00CEDC5C /* SoundCalls.h */, ); name = sounds; sourceTree = ""; @@ -365,6 +370,7 @@ buildActionMask = 2147483647; files = ( 74EEBAEF1E2D13A6004C6C65 /* AppDelegate.swift in Sources */, + ACC6F8382121E8DF00CEDC5C /* SoundCalls.m in Sources */, 74C3AE2E1E2E2A40003C07F2 /* creditscode.cpp in Sources */, 74C3AE2F1E2E2A40003C07F2 /* gamecode.cpp in Sources */, AC67C87D211C8DD3003AA164 /* NativeSoundCalls.swift in Sources */, diff --git a/proj.ios/SoundCalls.h b/proj.ios/SoundCalls.h new file mode 100644 index 0000000..a643d7c --- /dev/null +++ b/proj.ios/SoundCalls.h @@ -0,0 +1,16 @@ +#import +#import + +#define ShareSoundCalls [SoundCalls shareInstance] + +@interface SoundCalls : NSObject + ++ (instancetype)shareInstance; + +- (void)playBackgroundSound; +- (void)stopBackgroundSound; + +- (void)playGunshotSound; +- (void)stopGunshotSound; + +@end diff --git a/proj.ios/SoundCalls.m b/proj.ios/SoundCalls.m new file mode 100644 index 0000000..e4dce98 --- /dev/null +++ b/proj.ios/SoundCalls.m @@ -0,0 +1,49 @@ +#import "SoundCalls.h" + +@interface SoundCalls () + +@property (nonatomic, strong) AVAudioPlayer* backgroundPlayer; + +@property (nonatomic, strong) AVAudioPlayer* gunshotPlayer; + +@end + +@implementation SoundCalls + +static SoundCalls *_shareInstance; + ++(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 +