diff --git a/game/Sounds.cpp b/game/Sounds.cpp new file mode 100755 index 0000000..3c2f1d2 --- /dev/null +++ b/game/Sounds.cpp @@ -0,0 +1,34 @@ +#include "Sounds.h" +#include + +static JNIEnv* env = NULL; +static jclass jSounds = NULL; + +/** BackgroundSound */ + +void playBackgroundSound() { + jmethodID method = env->GetStaticMethodID(jSounds, "JniPlayBackgroundSound", "()V"); + env->CallStaticVoidMethod(jSounds, method); +} + +void stopBackgroundSound () { + jmethodID method = env->GetStaticMethodID(jSounds, "JniStopBackgroundSound", "()V"); + env->CallStaticVoidMethod(jSounds, method); +} + +/** GameSound - Gunshot */ + +void playGameSoundGunshot() { + jmethodID method = env->GetStaticMethodID(jSounds, "JniPlayGunshotSound", "()V"); + env->CallStaticVoidMethod(jSounds, method); +} + +void stopGameSoundGunshot() { + jmethodID method = env->GetStaticMethodID(jSounds, "JniStopGunshotSound", "()V"); + env->CallStaticVoidMethod(jSounds, method); +} + +JNIEXPORT void JNICALL Java_fishrungames_doublehitballs_sounds_JniSoundCalls_initJniSounds(JNIEnv *pEnv, jobject pThis) { + env = pEnv; + jSounds = env->FindClass("fishrungames/doublehitballs/sounds/JniSoundCalls"); +} \ No newline at end of file diff --git a/game/Sounds.h b/game/Sounds.h new file mode 100755 index 0000000..66f7b95 --- /dev/null +++ b/game/Sounds.h @@ -0,0 +1,7 @@ +#include + +extern "C" { + +JNIEXPORT void JNICALL Java_fishrungames_doublehitballs_sounds_JniSoundCalls_initJniSounds(JNIEnv *pEnv, jobject pThis); + +} diff --git a/proj.android-studio/app/CMakeLists.txt b/proj.android-studio/app/CMakeLists.txt index ca5c17d..f814c6c 100755 --- a/proj.android-studio/app/CMakeLists.txt +++ b/proj.android-studio/app/CMakeLists.txt @@ -1,38 +1,15 @@ -# Sets the minimum version of CMake required to build the native -# library. You should either keep the default value or only pass a -# value of 3.4.0 or lower. - cmake_minimum_required(VERSION 3.4.1) -# Creates and names a library, sets it as either STATIC -# or SHARED, and provides the relative paths to its source code. -# You can define multiple libraries, and CMake builds it for you. -# Gradle automatically packages shared libraries with your APK. - - -add_definitions(-DTARGET_ANDROID) - set(BOOST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../boost_1_67_0) - set(BOOST_GIL_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/boost-gil-extension) - set(ZIP_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/julienr-libzip-android/jni) - set(LIBPNG_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/libpng_1.4.1_android) - -set(LIBJPEG_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/jpeg-9") - - +set(LIBJPEG_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/jpeg-9) set(EIGEN_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../eigen) - set(SOL2_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../sol2) - set(LUA_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/lua-5.3.4/src) - - include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../tes-engine) - include_directories(${BOOST_PATH}) include_directories(${EIGEN_PATH}) include_directories(${SOL2_PATH}) @@ -40,75 +17,45 @@ include_directories(${LUA_PATH}) include_directories(${LIBPNG_PATH}) include_directories(${LIBJPEG_PATH}) include_directories(${ZIP_PATH}) - include_directories(${BOOST_GIL_PATH}) -add_library( engine - SHARED - IMPORTED ) +add_definitions(-DTARGET_ANDROID) + +add_library(engine SHARED IMPORTED + +) -set_target_properties( # Specifies the target library. - engine +set_target_properties(engine PROPERTIES IMPORTED_LOCATION - # Specifies the parameter you want to define. - PROPERTIES IMPORTED_LOCATION + ${CMAKE_CURRENT_SOURCE_DIR}/../../../tes-engine/SalmonEngineAndroid/app/build/intermediates/cmake/debug/obj/${ANDROID_ABI}/libengine.so - # Provides the path to the library you want to import. - ${CMAKE_CURRENT_SOURCE_DIR}/../../../tes-engine/SalmonEngineAndroid/app/build/intermediates/cmake/debug/obj/${ANDROID_ABI}/libengine.so ) - +) +add_library(DoubleHitBalls SHARED + ${CMAKE_CURRENT_SOURCE_DIR}/../../game/android_api.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../game/main_code.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../game/creditscode.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../game/galaxy.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../game/galaxy_menu.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../game/galaxy_objects.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../game/gamecode.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../game/loadingcode.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../game/menucode.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../game/Sounds.cpp -add_library( # Sets the name of the library. - DoubleHitBalls +) - # Sets the library as a shared library. - SHARED - - # Provides a relative path to your source file(s). - # Associated headers in the same location as their source - # file are automatically included. - ${CMAKE_CURRENT_SOURCE_DIR}/../../game/android_api.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/../../game/main_code.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/../../game/creditscode.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/../../game/galaxy.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/../../game/galaxy_menu.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/../../game/galaxy_objects.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/../../game/gamecode.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/../../game/loadingcode.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/../../game/menucode.cpp - ) - - - -# Searches for a specified prebuilt library and stores the path as a -# variable. Because system libraries are included in the search path by -# default, you only need to specify the name of the public NDK library -# you want to add. CMake verifies that the library exists before -# completing its build. - -find_library( # Sets the name of the path variable. - log-lib - - # Specifies the name of the NDK library that - # you want CMake to locate. - log ) +find_library(log-lib + log +) -find_library( # Sets the name of the path variable. - GLESv2-lib +find_library(GLESv2-lib + GLESv2 +) - # Specifies the name of the NDK library that - # you want CMake to locate. - GLESv2 ) - - -# Specifies libraries CMake should link to your target library. You -# can link multiple libraries, such as libraries you define in the -# build script, prebuilt third-party libraries, or system libraries. - -target_link_libraries( # Specifies the target library. - DoubleHitBalls - - # Links the target library to the log library - # included in the NDK. - ${log-lib} ${GLESv2-lib} engine ) +target_link_libraries(DoubleHitBalls + engine + ${log-lib} + ${GLESv2-lib} +) diff --git a/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/GL2JNIActivity.java b/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/GL2JNIActivity.java index a1558e3..b0d7479 100755 --- a/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/GL2JNIActivity.java +++ b/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/GL2JNIActivity.java @@ -20,6 +20,8 @@ import android.view.MotionEvent; public class GL2JNIActivity extends Activity { + + private static GL2JNIActivity instance; GLView mView; @@ -28,7 +30,9 @@ public class GL2JNIActivity extends Activity { super.onCreate(icicle); - + + instance = this; + EngineWrapper.LoadSalmonEngineLibrary(); EngineWrapper.SetActivityInstance(this); EngineWrapper.SetupEnviroment(); @@ -88,4 +92,12 @@ public class GL2JNIActivity extends Activity EngineWrapper.ProcessKeyDown(keyCode, event); return super.onKeyDown(keyCode, event); } + + public static GL2JNIActivity getInstance() { + if (instance == null) { + throw new RuntimeException("error GL2JNIActivity getInstance() - you are trying to get activity instance when it is not created or already destroyed"); + } + return instance; + } + } \ No newline at end of file diff --git a/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/GLView.java b/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/GLView.java index ab012e3..a35a556 100755 --- a/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/GLView.java +++ b/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/GLView.java @@ -9,6 +9,7 @@ import android.opengl.GLSurfaceView; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; +import fishrungames.doublehitballs.sounds.JniSoundCalls; import fishrungames.salmonengineandroid.GLViewAncestor; import fishrungames.salmonengineandroid.EngineWrapper; @@ -59,6 +60,7 @@ class GLView extends GLViewAncestor public void onSurfaceChanged(GL10 gl, int width, int height) { JniWrapper.Init(width,height); + JniSoundCalls.init(); } public void onSurfaceCreated(GL10 gl, EGLConfig config) diff --git a/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/JniWrapper.java b/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/JniWrapper.java index 8e73337..d2b6498 100755 --- a/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/JniWrapper.java +++ b/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/JniWrapper.java @@ -6,7 +6,6 @@ public class JniWrapper System.loadLibrary("DoubleHitBalls"); } - public static native void Init(int width, int height); } \ No newline at end of file diff --git a/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/sounds/JniSoundCalls.java b/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/sounds/JniSoundCalls.java new file mode 100644 index 0000000..ebe046a --- /dev/null +++ b/proj.android-studio/app/src/main/java/fishrungames/doublehitballs/sounds/JniSoundCalls.java @@ -0,0 +1,49 @@ +package fishrungames.doublehitballs.sounds; + +import android.media.MediaPlayer; + +import fishrungames.doublehitballs.GL2JNIActivity; +import fishrungames.doublehitballs.R; + +/** + * example sounds for JNI calls + * @author Artem Budarin + * Created by Artem Budarin on 06.08.2018. + */ + +public class JniSoundCalls { + + native public static void initJniSounds(); + + private static MediaPlayer backgroundPlayer = null; + private static MediaPlayer gunshotPlayer = null; + + public static void init() { + initJniSounds(); + } + + public static void JniPlayBackgroundSound() { + backgroundPlayer = MediaPlayer.create(GL2JNIActivity.getInstance(), R.raw.background_sound); + backgroundPlayer.setLooping(true); + backgroundPlayer.start(); + } + + public static void JniStopBackgroundSound() { + if (backgroundPlayer != null) { + backgroundPlayer.stop(); + } + } + + public static void JniPlayGunshotSound() { + gunshotPlayer = MediaPlayer.create(GL2JNIActivity.getInstance(), R.raw.gunshot_sound); + gunshotPlayer.setLooping(false); + gunshotPlayer.start(); + } + + public static void JniStopGunshotSound() { + if (gunshotPlayer != null) { + gunshotPlayer.stop(); + } + } + +} diff --git a/proj.android-studio/app/src/main/res/raw/background_sound.mp3 b/proj.android-studio/app/src/main/res/raw/background_sound.mp3 new file mode 100755 index 0000000..ead9299 Binary files /dev/null and b/proj.android-studio/app/src/main/res/raw/background_sound.mp3 differ diff --git a/proj.android-studio/app/src/main/res/raw/gunshot_sound.mp3 b/proj.android-studio/app/src/main/res/raw/gunshot_sound.mp3 new file mode 100755 index 0000000..ae35066 Binary files /dev/null and b/proj.android-studio/app/src/main/res/raw/gunshot_sound.mp3 differ diff --git a/proj.ios/BridgeDelegate.h b/proj.ios/BridgeDelegate.h new file mode 100644 index 0000000..7f1ba99 --- /dev/null +++ b/proj.ios/BridgeDelegate.h @@ -0,0 +1,16 @@ +#ifndef BridgeDelegate_h +#define BridgeDelegate_h + +class BridgeDelegate +{ + +public: + + static void playBackgroundSound(); + static void stopBackgroundSound(); + static void playGunshotSound(); + static void stopGunshotSound(); + +}; + +#endif /* BridgeDelegate_h */ diff --git a/proj.ios/BridgeDelegate.mm b/proj.ios/BridgeDelegate.mm new file mode 100644 index 0000000..f1280b2 --- /dev/null +++ b/proj.ios/BridgeDelegate.mm @@ -0,0 +1,22 @@ +#include "BridgeDelegate.h" +#import "Sounds.h" + +void BridgeDelegate::playBackgroundSound() +{ + [ShareSounds playBackgroundSound]; +} + +void BridgeDelegate::stopBackgroundSound() +{ + [ShareSounds stopBackgroundSound]; +} + +void BridgeDelegate::playGunshotSound() +{ + [ShareSounds playGunshotSound]; +} + +void BridgeDelegate::stopGunshotSound() +{ + [ShareSounds stopGunshotSound]; +} diff --git a/proj.ios/Double Hit Balls.xcodeproj/project.pbxproj b/proj.ios/Double Hit Balls.xcodeproj/project.pbxproj index 8e0c452..404b25f 100755 --- a/proj.ios/Double Hit Balls.xcodeproj/project.pbxproj +++ b/proj.ios/Double Hit Balls.xcodeproj/project.pbxproj @@ -32,6 +32,13 @@ 84D0FEBE1E274DDD00EC3FE5 /* libSalmon Engine.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C902A7815C5735700FBC901 /* libSalmon Engine.a */; }; 84D0FEC91E274E4A00EC3FE5 /* libvorbis-tremor-ios.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 84D0FEC61E274E3700EC3FE5 /* libvorbis-tremor-ios.a */; }; 84D0FECC1E274EBC00EC3FE5 /* main_code.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 84D0FECA1E274EBC00EC3FE5 /* main_code.cpp */; }; + AC67C87D211C8DD3003AA164 /* NativeSoundCalls.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC67C87C211C8DD3003AA164 /* NativeSoundCalls.swift */; }; + 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 */; }; + AC8032262122F8B100DEA5AC /* SoundCalls.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AC8032242122F8B100DEA5AC /* SoundCalls.cpp */; }; + AC80322A2122F8DD00DEA5AC /* BridgeDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = AC8032282122F8DC00DEA5AC /* BridgeDelegate.mm */; }; + ACC6F8382121E8DF00CEDC5C /* Sounds.m in Sources */ = {isa = PBXBuildFile; fileRef = ACC6F8372121E8DF00CEDC5C /* Sounds.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -103,6 +110,16 @@ 84D0FEC11E274E3700EC3FE5 /* vorbis-tremor-ios.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = "vorbis-tremor-ios.xcodeproj"; path = "../../libs/vorbis-tremor-ios/vorbis-tremor-ios.xcodeproj"; sourceTree = ""; }; 84D0FECA1E274EBC00EC3FE5 /* main_code.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = main_code.cpp; path = ../game/main_code.cpp; sourceTree = ""; }; 84D0FECB1E274EBC00EC3FE5 /* main_code.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = main_code.h; path = ../game/main_code.h; sourceTree = ""; }; + AC67C87C211C8DD3003AA164 /* NativeSoundCalls.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeSoundCalls.swift; sourceTree = ""; }; + 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 = ""; }; + AC8032242122F8B100DEA5AC /* SoundCalls.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SoundCalls.cpp; sourceTree = ""; }; + AC8032282122F8DC00DEA5AC /* BridgeDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = BridgeDelegate.mm; sourceTree = ""; }; + AC8032292122F8DD00DEA5AC /* BridgeDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BridgeDelegate.h; sourceTree = ""; }; + AC80322B2122FBC100DEA5AC /* SoundCalls.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SoundCalls.h; sourceTree = ""; }; + ACC6F8372121E8DF00CEDC5C /* Sounds.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Sounds.m; sourceTree = ""; }; + ACC6F8392121E8ED00CEDC5C /* Sounds.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Sounds.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -128,6 +145,8 @@ 4C49B2AE15B0991B003512CD = { isa = PBXGroup; children = ( + AC67C881211C8E24003AA164 /* background_sound.mp3 */, + AC67C880211C8E24003AA164 /* gunshot_sound.mp3 */, 84D0FEC11E274E3700EC3FE5 /* vorbis-tremor-ios.xcodeproj */, 4C902A7015C5735500FBC901 /* Salmon Engine.xcodeproj */, 4C7B819515C40F770024D61A /* Libs */, @@ -217,6 +236,8 @@ 4CE6A9E315B2F9A4006A3965 /* Game */ = { isa = PBXGroup; children = ( + AC8032272122F8C500DEA5AC /* Bridages */, + AC67C87B211C8DB6003AA164 /* sounds */, 74C3AE251E2E2A40003C07F2 /* creditscode.cpp */, 74C3AE261E2E2A40003C07F2 /* creditscode.h */, 74C3AE271E2E2A40003C07F2 /* game_area_interface.h */, @@ -228,6 +249,8 @@ 74C3AE2D1E2E2A40003C07F2 /* menucode.h */, 84D0FECA1E274EBC00EC3FE5 /* main_code.cpp */, 84D0FECB1E274EBC00EC3FE5 /* main_code.h */, + AC8032242122F8B100DEA5AC /* SoundCalls.cpp */, + AC80322B2122FBC100DEA5AC /* SoundCalls.h */, ); name = Game; sourceTree = ""; @@ -240,6 +263,26 @@ name = Products; sourceTree = ""; }; + AC67C87B211C8DB6003AA164 /* sounds */ = { + isa = PBXGroup; + children = ( + AC67C87C211C8DD3003AA164 /* NativeSoundCalls.swift */, + AC67C87E211C8DE3003AA164 /* NativeSoundCallsImpl.swift */, + ACC6F8372121E8DF00CEDC5C /* Sounds.m */, + ACC6F8392121E8ED00CEDC5C /* Sounds.h */, + ); + name = sounds; + sourceTree = ""; + }; + AC8032272122F8C500DEA5AC /* Bridages */ = { + isa = PBXGroup; + children = ( + AC8032292122F8DD00DEA5AC /* BridgeDelegate.h */, + AC8032282122F8DC00DEA5AC /* BridgeDelegate.mm */, + ); + name = Bridages; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -326,6 +369,8 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + AC67C882211C8E24003AA164 /* gunshot_sound.mp3 in Resources */, + AC67C883211C8E24003AA164 /* background_sound.mp3 in Resources */, 841270C91E639C2F00776DAE /* Images.xcassets in Resources */, 841D49651E65981600C85722 /* LaunchScreen.storyboard in Resources */, 4C49B2CC15B0991B003512CD /* InfoPlist.strings in Resources */, @@ -343,15 +388,20 @@ buildActionMask = 2147483647; files = ( 74EEBAEF1E2D13A6004C6C65 /* AppDelegate.swift in Sources */, + ACC6F8382121E8DF00CEDC5C /* Sounds.m in Sources */, 74C3AE2E1E2E2A40003C07F2 /* creditscode.cpp in Sources */, 74C3AE2F1E2E2A40003C07F2 /* gamecode.cpp in Sources */, + AC67C87D211C8DD3003AA164 /* NativeSoundCalls.swift in Sources */, + AC80322A2122F8DD00DEA5AC /* BridgeDelegate.mm in Sources */, 74EEBAF11E2D1C7C004C6C65 /* CustomGLKView.swift in Sources */, 84D0FECC1E274EBC00EC3FE5 /* main_code.cpp in Sources */, 74C3AE301E2E2A40003C07F2 /* loadingcode.cpp in Sources */, 74AC9EBD1E2CE407003C9749 /* ViewController.swift in Sources */, 74AC9EC11E2CF533003C9749 /* SENamespaceWrapper.cpp in Sources */, 74C3AE311E2E2A40003C07F2 /* menucode.cpp in Sources */, + AC67C87F211C8DE3003AA164 /* NativeSoundCallsImpl.swift in Sources */, 4C7AD44C15B1D77700A599F6 /* ios_api.cpp in Sources */, + AC8032262122F8B100DEA5AC /* SoundCalls.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/proj.ios/NativeSoundCalls.swift b/proj.ios/NativeSoundCalls.swift new file mode 100644 index 0000000..906b6da --- /dev/null +++ b/proj.ios/NativeSoundCalls.swift @@ -0,0 +1,11 @@ +import Foundation + +protocol NativeSoundCalls { + + func playBackgroundSound() + func stopBackgroundSound() + + func playGunshotSound() + func stopGunshotSound() + +} diff --git a/proj.ios/NativeSoundCallsImpl.swift b/proj.ios/NativeSoundCallsImpl.swift new file mode 100644 index 0000000..46696fb --- /dev/null +++ b/proj.ios/NativeSoundCallsImpl.swift @@ -0,0 +1,54 @@ +import Foundation +import AVFoundation + +// example sounds for native calls +// Created by Artem Budarin on 08.08.2018. + +public class NativeSoundCallsImpl : NativeSoundCalls { + + var backgroundPlayer: AVAudioPlayer? + var gunshotPlayer: AVAudioPlayer? + + func playBackgroundSound() { + guard let url = Bundle.main.url(forResource: "background_sound", withExtension: "mp3") else { return } + do { + try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) + try AVAudioSession.sharedInstance().setActive(true) + backgroundPlayer = try AVAudioPlayer(contentsOf: url) + + //swift 4 + //backgroundPlayer = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue) + + backgroundPlayer?.numberOfLoops = NSInteger.max + backgroundPlayer?.play() + } catch let error { + print(error.localizedDescription) + } + } + + func stopBackgroundSound() { + backgroundPlayer?.stop() + } + + func playGunshotSound() { + guard let url = Bundle.main.url(forResource: "gunshot_sound", withExtension: "mp3") else { return } + do { + try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) + try AVAudioSession.sharedInstance().setActive(true) + gunshotPlayer = try AVAudioPlayer(contentsOf: url) + + //swift 4 + //gunshotPlayer = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue) + + gunshotPlayer?.play() + } catch let error { + print(error.localizedDescription) + } + } + + func stopGunshotSound() { + gunshotPlayer?.stop() + } + +} + diff --git a/proj.ios/SoundCalls.cpp b/proj.ios/SoundCalls.cpp new file mode 100644 index 0000000..a007c37 --- /dev/null +++ b/proj.ios/SoundCalls.cpp @@ -0,0 +1,22 @@ +#include "SoundCalls.h" +#include "BridgeDelegate.h" + +void SoundCalls::playBackgroundSound() +{ + BridgeDelegate::playBackgroundSound(); +} + +void SoundCalls::stopBackgroundSound() +{ + BridgeDelegate::stopBackgroundSound(); +} + +void SoundCalls::playGunshotSound() +{ + BridgeDelegate::playGunshotSound(); +} + +void SoundCalls::stopGunshotSound() +{ + BridgeDelegate::stopGunshotSound(); +} diff --git a/proj.ios/SoundCalls.h b/proj.ios/SoundCalls.h new file mode 100644 index 0000000..76c5f6f --- /dev/null +++ b/proj.ios/SoundCalls.h @@ -0,0 +1,16 @@ +#ifndef __SOUND_CALLS_H__ +#define __SOUND_CALLS_H__ + +class SoundCalls +{ + +public: + + void playBackgroundSound(); + void stopBackgroundSound(); + void playGunshotSound(); + void stopGunshotSound(); + +}; + +#endif // __SOUND_CALLS_H__ diff --git a/proj.ios/Sounds.h b/proj.ios/Sounds.h new file mode 100644 index 0000000..fe1f701 --- /dev/null +++ b/proj.ios/Sounds.h @@ -0,0 +1,16 @@ +#import +#import + +#define ShareSounds [Sounds shareInstance] + +@interface Sounds : NSObject + ++ (instancetype)shareInstance; + +- (void)playBackgroundSound; +- (void)stopBackgroundSound; + +- (void)playGunshotSound; +- (void)stopGunshotSound; + +@end diff --git a/proj.ios/Sounds.m b/proj.ios/Sounds.m new file mode 100644 index 0000000..6f0c609 --- /dev/null +++ b/proj.ios/Sounds.m @@ -0,0 +1,49 @@ +#import "Sounds.h" + +@interface Sounds () + +@property (nonatomic, strong) AVAudioPlayer* backgroundPlayer; + +@property (nonatomic, strong) AVAudioPlayer* gunshotPlayer; + +@end + +@implementation Sounds + +static Sounds *_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 + diff --git a/proj.ios/background_sound.mp3 b/proj.ios/background_sound.mp3 new file mode 100755 index 0000000..ead9299 Binary files /dev/null and b/proj.ios/background_sound.mp3 differ diff --git a/proj.ios/gunshot_sound.mp3 b/proj.ios/gunshot_sound.mp3 new file mode 100755 index 0000000..ae35066 Binary files /dev/null and b/proj.ios/gunshot_sound.mp3 differ