Skip to content

Commit

Permalink
add init api
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuxiang committed Aug 10, 2017
1 parent be1c7ed commit 37b14f0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $ react-native link react-native-recording
```javascript
import Recording from 'react-native-recording'

Recording.start(8000, 1024)
Recording.init(8000, 1024)
Recording.start()
Recording.on('recording', data => console.log(data))
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import com.facebook.react.modules.core.DeviceEventManagerModule;

class RecordingModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
private static AudioRecord audioRecord;
private boolean isRecording;
private final ReactApplicationContext reactContext;
private boolean running;
private int bufferSize;
private Thread recordingThread;

Expand All @@ -29,8 +29,8 @@ public String getName() {
}

@ReactMethod
public void start(int sampleRate, int bufferSize) {
if (isRecording || (recordingThread != null && recordingThread.isAlive())) {
public void init(int sampleRate, int bufferSize) {
if (running || (recordingThread != null && recordingThread.isAlive())) {
return;
}

Expand All @@ -39,35 +39,44 @@ public void start(int sampleRate, int bufferSize) {
audioRecord.release();
}

this.bufferSize = bufferSize;
audioRecord = new AudioRecord(
MediaRecorder.AudioSource.MIC,
sampleRate,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSize * 2);
audioRecord.startRecording();
isRecording = true;

recordingThread = new Thread(new Runnable() {
public void run() {
recording();
}
}, "RecordingThread");
recordingThread.start();

this.bufferSize = bufferSize;
}

@ReactMethod
public void start() {
if (!running && audioRecord != null && recordingThread != null) {
running = true;
audioRecord.startRecording();
recordingThread.start();
}
}

@ReactMethod
public void stop() {
if (audioRecord != null) {
isRecording = false;
running = false;
audioRecord.stop();
audioRecord.release();
audioRecord = null;
}
}

private void recording() {
short data[] = new short[bufferSize];
while (isRecording && !reactContext.getCatalystInstance().isDestroyed()) {
while (running && !reactContext.getCatalystInstance().isDestroyed()) {
WritableArray array = Arguments.createArray();
audioRecord.read(data, 0, bufferSize);
for (float value : data) {
Expand Down
3 changes: 2 additions & 1 deletion example/src/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Recording from 'react-native-recording'

export default class Main extends Component {
componentDidMount() {
Recording.start(8000, 1024)
Recording.init(8000, 1024)
Recording.start()
Recording.on('recording', data => {
if (this.webView) {
this.webView.postMessage(data)
Expand Down
15 changes: 6 additions & 9 deletions ios/Recording.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
@implementation Recording {
AudioQueueRef _queue;
AudioQueueBufferRef _buffer;
NSNumber *_audioData[4096];
NSNumber *_audioData[8192];
UInt32 _bufferSize;
bool _isRecording;
}

void inputCallback(
Expand All @@ -20,10 +19,8 @@ void inputCallback(

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(start:(int)sampleRate bufferSize:(int)bufferSize) {
if (_isRecording) {
return;
}
RCT_EXPORT_METHOD(init:(int)sampleRate bufferSize:(int)bufferSize) {
_bufferSize = (UInt32) bufferSize;

AudioStreamBasicDescription description;
description.mReserved = 0;
Expand All @@ -39,10 +36,10 @@ void inputCallback(
AudioQueueNewInput(&description, inputCallback, (__bridge void *) self, NULL, NULL, 0, &_queue);
AudioQueueAllocateBuffer(_queue, (UInt32) (bufferSize * 2), &_buffer);
AudioQueueEnqueueBuffer(_queue, _buffer, 0, NULL);
AudioQueueStart(_queue, NULL);
}

_bufferSize = (UInt32) bufferSize;
_isRecording = true;
RCT_EXPORT_METHOD(start) {
AudioQueueStart(_queue, NULL);
}

RCT_EXPORT_METHOD(stop) {
Expand Down

0 comments on commit 37b14f0

Please sign in to comment.