From 8b160f6dc00c4e6eaef98234d5cfe478b809bd3a Mon Sep 17 00:00:00 2001 From: Cymen Vig Date: Mon, 10 Aug 2015 22:23:19 -0700 Subject: [PATCH 1/5] Fix RNAudioPlayer require --- AudioPlayer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AudioPlayer.js b/AudioPlayer.js index 048a5ce..a18a7f0 100644 --- a/AudioPlayer.js +++ b/AudioPlayer.js @@ -1,6 +1,6 @@ 'use strict'; -var RNAudioPlayer = require('NativeModules').RNAudioPlayer; +var RNAudioPlayer = require('react-native').NativeModules.RNAudioPlayer; var AudioPlayer = { play(fileName: string) { From 04c86085fe537282bad32589d3da538a838a1654 Mon Sep 17 00:00:00 2001 From: Cymen Vig Date: Tue, 11 Aug 2015 00:52:13 -0700 Subject: [PATCH 2/5] Pull in RCTEventDispatcher.h --- RNAudioPlayer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/RNAudioPlayer.h b/RNAudioPlayer.h index 4eb3b9b..883d8ca 100644 --- a/RNAudioPlayer.h +++ b/RNAudioPlayer.h @@ -1,4 +1,5 @@ #import "RCTBridgeModule.h" +#import "RCTEventDispatcher.h" #import @interface RNAudioPlayer : NSObject From a79b84ea49f62d0e4170acb93a0c105683fa44d2 Mon Sep 17 00:00:00 2001 From: Cymen Vig Date: Tue, 11 Aug 2015 00:52:32 -0700 Subject: [PATCH 3/5] Add pause, play can unpause, stop, and playback finished event * pause allows us to pause the playback * play is refactored to allow unpausing (if no filename is passed) * stop can stop the playback * add event notification for playback finished --- RNAudioPlayer.m | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/RNAudioPlayer.m b/RNAudioPlayer.m index 8572672..20b808e 100644 --- a/RNAudioPlayer.m +++ b/RNAudioPlayer.m @@ -1,21 +1,53 @@ #import "RNAudioPlayer.h" +#import "RCTBridge.h" +#import "RCTEventDispatcher.h" @implementation RNAudioPlayer RCT_EXPORT_MODULE() -RCT_EXPORT_METHOD(play:(NSString *)fileName) +@synthesize bridge = _bridge; + +- (instancetype)init { - AVAudioSession *session = [AVAudioSession sharedInstance]; - [session setCategory: AVAudioSessionCategoryPlayback error: nil]; - [session setActive: YES error: nil]; + self = [super init]; + + if (self) { + AVAudioSession *session = [AVAudioSession sharedInstance]; + [session setCategory: AVAudioSessionCategoryPlayback error: nil]; + [session setActive: YES error: nil]; + } - NSURL *soundURL = [[NSBundle mainBundle] URLForResource:[[fileName lastPathComponent] stringByDeletingPathExtension] + return self; +} + +RCT_EXPORT_METHOD(play:(NSString *)fileName) +{ + if (fileName) { + NSURL *soundURL = [[NSBundle mainBundle] URLForResource:[[fileName lastPathComponent] stringByDeletingPathExtension] withExtension:[fileName pathExtension]]; - self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil]; + self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil]; + self.audioPlayer.delegate = self; + } [self.audioPlayer play]; } + +RCT_EXPORT_METHOD(pause) +{ + [self.audioPlayer pause]; +} + +RCT_EXPORT_METHOD(stop) +{ + [self.audioPlayer stop]; +} + + +- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { + [self.bridge.eventDispatcher sendAppEventWithName:@"AudioPlayerDidFinishPlaying" body:nil]; +} + @end From 1deba1c1e8626aac5964e32023e16b1ae287c68d Mon Sep 17 00:00:00 2001 From: Cymen Vig Date: Tue, 11 Aug 2015 00:54:07 -0700 Subject: [PATCH 4/5] AudioPlayer.js has support for pause and stop and events --- AudioPlayer.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/AudioPlayer.js b/AudioPlayer.js index a18a7f0..d3e8a0d 100644 --- a/AudioPlayer.js +++ b/AudioPlayer.js @@ -1,11 +1,27 @@ 'use strict'; var RNAudioPlayer = require('react-native').NativeModules.RNAudioPlayer; +var NativeAppEventEmitter = require('react-native').NativeAppEventEmitter; + +module.exports = class AudioPlayer { + constructor(options) { + if (options && options.onPlaybackFinished) { + NativeAppEventEmitter.addListener( + 'AudioPlayerDidFinishPlaying', + options.onPlaybackFinished + ); + } + } -var AudioPlayer = { play(fileName: string) { RNAudioPlayer.play(fileName); } -}; -module.exports = AudioPlayer; + pause() { + RNAudioPlayer.pause(); + } + + stop() { + RNAudioPlayer.stop(); + } +}; From 77bcc7789124ab01976593c1f48b163a181fc737 Mon Sep 17 00:00:00 2001 From: Cymen Vig Date: Tue, 11 Aug 2015 00:55:43 -0700 Subject: [PATCH 5/5] Update README --- README.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0f4749e..e95e0d2 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ npm install react-native-audioplayer --save In XCode, in the project navigator, right click Libraries ➜ Add Files to [your project's name] Go to node_modules ➜ react-native-audioplayer and add the .xcodeproj file -In XCode, in the project navigator, select your project. Add the lib*.a from the audioplayer project to your project's Build Phases ➜ Link Binary With Libraries Click .xcodeproj file you added before in the project navigator and go the Build Settings tab. Make sure 'All' is toggled on (instead of 'Basic'). Look for Header Search Paths and make sure it contains both $(SRCROOT)/../react-native/React and $(SRCROOT)/../../React - mark both as recursive. +In XCode, in the project navigator, select your project. Add the lib*.a from the audioplayer project to your project's Build Phases ➜ Link Binary With Libraries. Click .xcodeproj file you added before in the project navigator and go the Build Settings tab. Make sure 'All' is toggled on (instead of 'Basic'). Look for Header Search Paths and make sure it contains both $(SRCROOT)/../react-native/React and $(SRCROOT)/../../React - mark both as recursive. Run your project (Cmd+R) @@ -26,10 +26,17 @@ The AudioPlayer API is exposed at AudioPlayer.play(soundName). The sound is play ```javascript -//require module +// require module var AudioPlayer = require('react-native-audioplayer'); -//play sound -AudioPlayer.play('beep.mp3'); +// create instance of player with callback for playback finished event +var audioPlayer = new AudioPlayer({ + onFinishedPlayback: function() { + console.log('playback finished!'); + } +}); + +// play sound +audioPlayer.play('beep.mp3'); ```