Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
add option to set audio source for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayr committed Jan 12, 2019
1 parent 5f79403 commit 87164c6
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Example/App1.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class App extends Component {
const p = await Permissions.check('microphone');
console.log('permission check', p);
if (p === 'authorized') return;
this.requestPermission();
return this.requestPermission();
};

requestPermission = async () => {
Expand Down
2 changes: 1 addition & 1 deletion Example/App2.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class App extends Component {
const p = await Permissions.check('microphone');
console.log('permission check', p);
if (p === 'authorized') return;
this.requestPermission();
return this.requestPermission();
};

requestPermission = async () => {
Expand Down
2 changes: 1 addition & 1 deletion Example/App3.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class App extends Component {
const p = await Permissions.check('microphone');
console.log('permission check', p);
if (p === 'authorized') return;
this.requestPermission();
return this.requestPermission();
};

requestPermission = async () => {
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const options = {
sampleRate: 16000, // default 44100
channels: 1, // 1 or 2, default 1
bitsPerSample: 16, // 8 or 16, default 16
audioSource: 6, // android only (see below)
wavFile: 'test.wav' // default 'audio.wav'
};

Expand All @@ -32,6 +33,8 @@ AudioRecord.on('data', data => {
});
```

For `audioSource` use one of the constant values from [here](https://developer.android.com/reference/android/media/MediaRecorder.AudioSource). Default value is 6 (`VOICE_RECOGNITION`).

Use 3rd-party module like [buffer](https://www.npmjs.com/package/buffer) to decode base64 data. Example -
```js
// yarn add buffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class RNAudioRecordModule extends ReactContextBaseJavaModule {
private int sampleRateInHz;
private int channelConfig;
private int audioFormat;
private int audioSource;

private AudioRecord recorder;
private int bufferSize;
Expand Down Expand Up @@ -66,6 +67,11 @@ public void init(ReadableMap options) {
}
}

audioSource = AudioSource.VOICE_RECOGNITION;
if (options.hasKey("audioSource")) {
audioSource = options.getInt("audioSource");
}

String documentDirectoryPath = getReactApplicationContext().getFilesDir().getAbsolutePath();
outFile = documentDirectoryPath + "/" + "audio.wav";
tmpFile = documentDirectoryPath + "/" + "temp.pcm";
Expand All @@ -79,7 +85,7 @@ public void init(ReadableMap options) {

bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
int recordingBufferSize = bufferSize * 3;
recorder = new AudioRecord(AudioSource.VOICE_RECOGNITION, sampleRateInHz, channelConfig, audioFormat, recordingBufferSize);
recorder = new AudioRecord(audioSource, sampleRateInHz, channelConfig, audioFormat, recordingBufferSize);
}

@ReactMethod
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-audio-record",
"version": "0.1.3",
"version": "0.2.0",
"description": "Audio record buffers for React Native",
"author": "Vinay",
"license": "MIT",
Expand Down

0 comments on commit 87164c6

Please sign in to comment.