├── simple_sound.js
├── RNSimpleSound.xcodeproj
├── project.xcworkspace
│ └── contents.xcworkspacedata
└── project.pbxproj
├── RNSimpleSound
├── RNSimpleSound.h
└── RNSimpleSound.m
├── package.json
├── LICENSE.txt
└── README.md
/simple_sound.js:
--------------------------------------------------------------------------------
1 | module.exports = require('react-native').NativeModules.RNSimpleSound;
2 |
--------------------------------------------------------------------------------
/RNSimpleSound.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/RNSimpleSound/RNSimpleSound.h:
--------------------------------------------------------------------------------
1 | //
2 | // RNSimpleSound.h
3 | // RNSimpleSound
4 | //
5 | // Copyright © 2015 mikehedman. All rights reserved.
6 | //
7 |
8 | #import "RCTBridgeModule.h"
9 |
10 | @interface RNSimpleSound : NSObject
11 |
12 | @end
13 |
14 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-native-simple-sound",
3 | "version": "1.1.0",
4 | "description": "Start, stop, and pause a sound. iOS only. Derived from https://github.com/zmxv/react-native-sound",
5 | "main": "simple_sound.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "https://github.com/mikehedman/react-native-simple-sound.git"
12 | },
13 | "keywords": [
14 | "react-component",
15 | "react-native",
16 | "ios",
17 | "sound",
18 | "audio"
19 | ],
20 | "author": "Mike Hedman",
21 | "license": "MIT",
22 | "bugs": {
23 | "url": "https://github.com/mikehedman/react-native-simple-sound/issues"
24 | },
25 | "homepage": "https://github.com/mikehedman/react-native-simple-sound"
26 | }
27 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Mike Hedman
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/RNSimpleSound/RNSimpleSound.m:
--------------------------------------------------------------------------------
1 | //
2 | // RNSimpleSound.m
3 | // RNSimpleSound
4 | //
5 | // Copyright © 2015 mikehedman. All rights reserved.
6 | //
7 |
8 | #import "RNSimpleSound.h"
9 | #import
10 |
11 | @implementation RNSimpleSound {
12 | BOOL _enabled;
13 | AVAudioPlayer* player;
14 | }
15 |
16 | -(NSURL*) soundURL:(NSString*)fileName {
17 | return [[NSBundle mainBundle] URLForResource:[[fileName lastPathComponent]stringByDeletingPathExtension]
18 | withExtension:[fileName pathExtension]];
19 | }
20 |
21 | RCT_EXPORT_MODULE();
22 |
23 | RCT_EXPORT_METHOD(enable:(BOOL)enabled) {
24 | _enabled = enabled;
25 | AVAudioSession *session = [AVAudioSession sharedInstance];
26 | [session setCategory: AVAudioSessionCategoryAmbient error: nil];
27 | [session setActive: enabled error: nil];
28 | }
29 |
30 | RCT_EXPORT_METHOD(prepare:(NSString *)fileName) {
31 | player = [[AVAudioPlayer alloc] initWithContentsOfURL:[self soundURL:fileName] error:nil];
32 | [player prepareToPlay];
33 | }
34 |
35 | RCT_EXPORT_METHOD(play) {
36 | if (!_enabled || !player.url) {
37 | return;
38 | }
39 |
40 | [player play];
41 | }
42 |
43 | RCT_EXPORT_METHOD(pause) {
44 | if (!_enabled) {
45 | return;
46 | }
47 | [player pause];
48 | }
49 |
50 | RCT_EXPORT_METHOD(stop) {
51 | if (!_enabled) {
52 | return;
53 | }
54 | [player stop];
55 | //stop does not reset the player, so force it
56 | [player prepareToPlay];
57 | }
58 |
59 | @end
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-native-simple-sound
2 |
3 | React Native module for playing, pausing, and stopping sound clips.
4 |
5 | Derived from [react-native-sound](https://github.com/zmxv/react-native-sound), which allows multiple sound files to be played at once, but does not offer pause or stop.
6 |
7 | ## Installation
8 |
9 | ```javascript
10 | npm install react-native-simple-sound --save
11 | ```
12 |
13 | In XCode, right click **Libraries**.
14 | Click **Add Files to "[Your project]"**.
15 | Navigate to **node_modules/react-native-simple-sound**.
16 | Add the file **RNSimpleSound.xcodeproj**.
17 |
18 | In the project navigator, select your project.
19 | Click the build target.
20 | Click **Build Phases**.
21 | Expand **Link Binary With Libraries**.
22 | Click the plus button and add **libRNSimpleSound.a** under **Workspace**.
23 |
24 | Run your project (⌘+R).
25 |
26 | ## Example
27 |
28 | ```js
29 | var Sound = require('react-native-simple-sound');
30 | Sound.enable(true); // Enable sound
31 | Sound.prepare('tap.aac'); // Preload the sound file 'tap.aac' in the app bundle
32 | Sound.play(); // Play the sound 'tap.aac'
33 | Sound.pause(); // Pause the sound
34 | Sound.play(); // Resume playing the sound
35 | Sound.stop(); // Stop and reset the sound.
36 | ```
37 |
38 | ## Notes
39 | - Sound.enable(true) must be called before playing any sound.
40 | - Sound.prepare(...) must be called before playing - preloads a sound file and prepares it for playback.
41 | - To change sounds, stop() the current sound and call prepare() with the new sound.
42 | - The module wraps AVAudioPlayer which supports aac, aiff, mp3, wav etc. The full list of supported formats can be found [here](https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html).
43 | - The stop() function resets the sound back to the beginning, this is different from the standard behavior of AVAudioPlayer
44 | - Sound files must be in the bundle (in your app's main folder in Xcode)
45 |
--------------------------------------------------------------------------------
/RNSimpleSound.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 46;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | 27D4F4331BED747600C15668 /* RNSimpleSound.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 27D4F4321BED747600C15668 /* RNSimpleSound.h */; };
11 | 27D4F43B1BED767D00C15668 /* RNSimpleSound.m in Sources */ = {isa = PBXBuildFile; fileRef = 27D4F4341BED747600C15668 /* RNSimpleSound.m */; };
12 | /* End PBXBuildFile section */
13 |
14 | /* Begin PBXCopyFilesBuildPhase section */
15 | 27D4F42D1BED747600C15668 /* CopyFiles */ = {
16 | isa = PBXCopyFilesBuildPhase;
17 | buildActionMask = 2147483647;
18 | dstPath = "include/$(PRODUCT_NAME)";
19 | dstSubfolderSpec = 16;
20 | files = (
21 | 27D4F4331BED747600C15668 /* RNSimpleSound.h in CopyFiles */,
22 | );
23 | runOnlyForDeploymentPostprocessing = 0;
24 | };
25 | /* End PBXCopyFilesBuildPhase section */
26 |
27 | /* Begin PBXFileReference section */
28 | 27D4F42F1BED747600C15668 /* libRNSimpleSound.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNSimpleSound.a; sourceTree = BUILT_PRODUCTS_DIR; };
29 | 27D4F4321BED747600C15668 /* RNSimpleSound.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNSimpleSound.h; sourceTree = ""; };
30 | 27D4F4341BED747600C15668 /* RNSimpleSound.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNSimpleSound.m; sourceTree = ""; };
31 | /* End PBXFileReference section */
32 |
33 | /* Begin PBXFrameworksBuildPhase section */
34 | 27D4F42C1BED747600C15668 /* Frameworks */ = {
35 | isa = PBXFrameworksBuildPhase;
36 | buildActionMask = 2147483647;
37 | files = (
38 | );
39 | runOnlyForDeploymentPostprocessing = 0;
40 | };
41 | /* End PBXFrameworksBuildPhase section */
42 |
43 | /* Begin PBXGroup section */
44 | 27D4F4261BED747600C15668 = {
45 | isa = PBXGroup;
46 | children = (
47 | 27D4F4311BED747600C15668 /* RNSimpleSound */,
48 | 27D4F4301BED747600C15668 /* Products */,
49 | );
50 | sourceTree = "";
51 | };
52 | 27D4F4301BED747600C15668 /* Products */ = {
53 | isa = PBXGroup;
54 | children = (
55 | 27D4F42F1BED747600C15668 /* libRNSimpleSound.a */,
56 | );
57 | name = Products;
58 | sourceTree = "";
59 | };
60 | 27D4F4311BED747600C15668 /* RNSimpleSound */ = {
61 | isa = PBXGroup;
62 | children = (
63 | 27D4F4321BED747600C15668 /* RNSimpleSound.h */,
64 | 27D4F4341BED747600C15668 /* RNSimpleSound.m */,
65 | );
66 | path = RNSimpleSound;
67 | sourceTree = "";
68 | };
69 | /* End PBXGroup section */
70 |
71 | /* Begin PBXNativeTarget section */
72 | 27D4F42E1BED747600C15668 /* RNSimpleSound */ = {
73 | isa = PBXNativeTarget;
74 | buildConfigurationList = 27D4F4381BED747600C15668 /* Build configuration list for PBXNativeTarget "RNSimpleSound" */;
75 | buildPhases = (
76 | 27D4F42B1BED747600C15668 /* Sources */,
77 | 27D4F42C1BED747600C15668 /* Frameworks */,
78 | 27D4F42D1BED747600C15668 /* CopyFiles */,
79 | );
80 | buildRules = (
81 | );
82 | dependencies = (
83 | );
84 | name = RNSimpleSound;
85 | productName = RNSimpleSound;
86 | productReference = 27D4F42F1BED747600C15668 /* libRNSimpleSound.a */;
87 | productType = "com.apple.product-type.library.static";
88 | };
89 | /* End PBXNativeTarget section */
90 |
91 | /* Begin PBXProject section */
92 | 27D4F4271BED747600C15668 /* Project object */ = {
93 | isa = PBXProject;
94 | attributes = {
95 | LastUpgradeCheck = 0710;
96 | ORGANIZATIONNAME = mikehedman;
97 | TargetAttributes = {
98 | 27D4F42E1BED747600C15668 = {
99 | CreatedOnToolsVersion = 7.1;
100 | };
101 | };
102 | };
103 | buildConfigurationList = 27D4F42A1BED747600C15668 /* Build configuration list for PBXProject "RNSimpleSound" */;
104 | compatibilityVersion = "Xcode 3.2";
105 | developmentRegion = English;
106 | hasScannedForEncodings = 0;
107 | knownRegions = (
108 | en,
109 | );
110 | mainGroup = 27D4F4261BED747600C15668;
111 | productRefGroup = 27D4F4301BED747600C15668 /* Products */;
112 | projectDirPath = "";
113 | projectRoot = "";
114 | targets = (
115 | 27D4F42E1BED747600C15668 /* RNSimpleSound */,
116 | );
117 | };
118 | /* End PBXProject section */
119 |
120 | /* Begin PBXSourcesBuildPhase section */
121 | 27D4F42B1BED747600C15668 /* Sources */ = {
122 | isa = PBXSourcesBuildPhase;
123 | buildActionMask = 2147483647;
124 | files = (
125 | 27D4F43B1BED767D00C15668 /* RNSimpleSound.m in Sources */,
126 | );
127 | runOnlyForDeploymentPostprocessing = 0;
128 | };
129 | /* End PBXSourcesBuildPhase section */
130 |
131 | /* Begin XCBuildConfiguration section */
132 | 27D4F4361BED747600C15668 /* Debug */ = {
133 | isa = XCBuildConfiguration;
134 | buildSettings = {
135 | ALWAYS_SEARCH_USER_PATHS = NO;
136 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
137 | CLANG_CXX_LIBRARY = "libc++";
138 | CLANG_ENABLE_MODULES = YES;
139 | CLANG_ENABLE_OBJC_ARC = YES;
140 | CLANG_WARN_BOOL_CONVERSION = YES;
141 | CLANG_WARN_CONSTANT_CONVERSION = YES;
142 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
143 | CLANG_WARN_EMPTY_BODY = YES;
144 | CLANG_WARN_ENUM_CONVERSION = YES;
145 | CLANG_WARN_INT_CONVERSION = YES;
146 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
147 | CLANG_WARN_UNREACHABLE_CODE = YES;
148 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
149 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
150 | COPY_PHASE_STRIP = NO;
151 | DEBUG_INFORMATION_FORMAT = dwarf;
152 | ENABLE_STRICT_OBJC_MSGSEND = YES;
153 | ENABLE_TESTABILITY = YES;
154 | GCC_C_LANGUAGE_STANDARD = gnu99;
155 | GCC_DYNAMIC_NO_PIC = NO;
156 | GCC_NO_COMMON_BLOCKS = YES;
157 | GCC_OPTIMIZATION_LEVEL = 0;
158 | GCC_PREPROCESSOR_DEFINITIONS = (
159 | "DEBUG=1",
160 | "$(inherited)",
161 | );
162 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
163 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
164 | GCC_WARN_UNDECLARED_SELECTOR = YES;
165 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
166 | GCC_WARN_UNUSED_FUNCTION = YES;
167 | GCC_WARN_UNUSED_VARIABLE = YES;
168 | IPHONEOS_DEPLOYMENT_TARGET = 9.1;
169 | MTL_ENABLE_DEBUG_INFO = YES;
170 | ONLY_ACTIVE_ARCH = YES;
171 | SDKROOT = iphoneos;
172 | };
173 | name = Debug;
174 | };
175 | 27D4F4371BED747600C15668 /* Release */ = {
176 | isa = XCBuildConfiguration;
177 | buildSettings = {
178 | ALWAYS_SEARCH_USER_PATHS = NO;
179 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
180 | CLANG_CXX_LIBRARY = "libc++";
181 | CLANG_ENABLE_MODULES = YES;
182 | CLANG_ENABLE_OBJC_ARC = YES;
183 | CLANG_WARN_BOOL_CONVERSION = YES;
184 | CLANG_WARN_CONSTANT_CONVERSION = YES;
185 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
186 | CLANG_WARN_EMPTY_BODY = YES;
187 | CLANG_WARN_ENUM_CONVERSION = YES;
188 | CLANG_WARN_INT_CONVERSION = YES;
189 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
190 | CLANG_WARN_UNREACHABLE_CODE = YES;
191 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
192 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
193 | COPY_PHASE_STRIP = NO;
194 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
195 | ENABLE_NS_ASSERTIONS = NO;
196 | ENABLE_STRICT_OBJC_MSGSEND = YES;
197 | GCC_C_LANGUAGE_STANDARD = gnu99;
198 | GCC_NO_COMMON_BLOCKS = YES;
199 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
200 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
201 | GCC_WARN_UNDECLARED_SELECTOR = YES;
202 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
203 | GCC_WARN_UNUSED_FUNCTION = YES;
204 | GCC_WARN_UNUSED_VARIABLE = YES;
205 | IPHONEOS_DEPLOYMENT_TARGET = 9.1;
206 | MTL_ENABLE_DEBUG_INFO = NO;
207 | SDKROOT = iphoneos;
208 | VALIDATE_PRODUCT = YES;
209 | };
210 | name = Release;
211 | };
212 | 27D4F4391BED747600C15668 /* Debug */ = {
213 | isa = XCBuildConfiguration;
214 | buildSettings = {
215 | "HEADER_SEARCH_PATHS[arch=*]" = (
216 | "$(SRCROOT)/../react-native/React/**",
217 | "$(SRCROOT)/../../React/**",
218 | );
219 | OTHER_LDFLAGS = "-ObjC";
220 | PRODUCT_NAME = "$(TARGET_NAME)";
221 | SKIP_INSTALL = YES;
222 | };
223 | name = Debug;
224 | };
225 | 27D4F43A1BED747600C15668 /* Release */ = {
226 | isa = XCBuildConfiguration;
227 | buildSettings = {
228 | "HEADER_SEARCH_PATHS[arch=*]" = (
229 | "$(SRCROOT)/../react-native/React/**",
230 | "$(SRCROOT)/../../React/**",
231 | );
232 | OTHER_LDFLAGS = "-ObjC";
233 | PRODUCT_NAME = "$(TARGET_NAME)";
234 | SKIP_INSTALL = YES;
235 | };
236 | name = Release;
237 | };
238 | /* End XCBuildConfiguration section */
239 |
240 | /* Begin XCConfigurationList section */
241 | 27D4F42A1BED747600C15668 /* Build configuration list for PBXProject "RNSimpleSound" */ = {
242 | isa = XCConfigurationList;
243 | buildConfigurations = (
244 | 27D4F4361BED747600C15668 /* Debug */,
245 | 27D4F4371BED747600C15668 /* Release */,
246 | );
247 | defaultConfigurationIsVisible = 0;
248 | defaultConfigurationName = Release;
249 | };
250 | 27D4F4381BED747600C15668 /* Build configuration list for PBXNativeTarget "RNSimpleSound" */ = {
251 | isa = XCConfigurationList;
252 | buildConfigurations = (
253 | 27D4F4391BED747600C15668 /* Debug */,
254 | 27D4F43A1BED747600C15668 /* Release */,
255 | );
256 | defaultConfigurationIsVisible = 0;
257 | defaultConfigurationName = Release;
258 | };
259 | /* End XCConfigurationList section */
260 | };
261 | rootObject = 27D4F4271BED747600C15668 /* Project object */;
262 | }
263 |
--------------------------------------------------------------------------------