added sound players from obj-c

This commit is contained in:
Artem Budarin 2018-08-13 21:29:26 +05:00
parent 4ff8b0b88a
commit 584b48c3ab
3 changed files with 71 additions and 0 deletions

View File

@ -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 = "<group>"; };
AC67C880211C8E24003AA164 /* gunshot_sound.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = gunshot_sound.mp3; sourceTree = "<group>"; };
AC67C881211C8E24003AA164 /* background_sound.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = background_sound.mp3; sourceTree = "<group>"; };
ACC6F8372121E8DF00CEDC5C /* SoundCalls.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SoundCalls.m; sourceTree = "<group>"; };
ACC6F8392121E8ED00CEDC5C /* SoundCalls.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SoundCalls.h; sourceTree = "<group>"; };
/* 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 = "<group>";
@ -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 */,

16
proj.ios/SoundCalls.h Normal file
View File

@ -0,0 +1,16 @@
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#define ShareSoundCalls [SoundCalls shareInstance]
@interface SoundCalls : NSObject
+ (instancetype)shareInstance;
- (void)playBackgroundSound;
- (void)stopBackgroundSound;
- (void)playGunshotSound;
- (void)stopGunshotSound;
@end

49
proj.ios/SoundCalls.m Normal file
View File

@ -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