Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions AudioPlayer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
'use strict';

var RNAudioPlayer = require('NativeModules').RNAudioPlayer;
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();
}
};
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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');

```
1 change: 1 addition & 0 deletions RNAudioPlayer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "RCTBridgeModule.h"
#import "RCTEventDispatcher.h"
#import <AVFoundation/AVFoundation.h>

@interface RNAudioPlayer : NSObject <RCTBridgeModule>
Expand Down
44 changes: 38 additions & 6 deletions RNAudioPlayer.m
Original file line number Diff line number Diff line change
@@ -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