Skip to content

Commit 3f1b069

Browse files
authored
Add props to allow download of recording blob (#36)
1 parent 6196269 commit 3f1b069

File tree

5 files changed

+56
-32
lines changed

5 files changed

+56
-32
lines changed

README.md

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,46 +28,37 @@ const addAudioElement = (blob) => {
2828

2929
ReactDOM.createRoot(document.getElementById("root")).render(
3030
<React.StrictMode>
31-
<AudioRecorder onRecordingComplete={addAudioElement} />
31+
<AudioRecorder
32+
onRecordingComplete={addAudioElement}
33+
downloadOnSavePress={true}
34+
downloadFileExtension="mp3"
35+
/>
3236
</React.StrictMode>
3337
);
3438
```
3539

36-
The component also takes a `classes` as a prop, allowing you to modify the styles for the entire component or specific portions of it.
37-
40+
| Props | Description | Default | Optional |
41+
| :------------ |:--------------- |:--------------- | :--------------- |
42+
| **`onRecordingComplete`** | A method that gets called when "Save recording" option is pressed | N/A | Yes |
43+
| **`downloadOnSavePress`** | A `boolean` value that determines if the recording should be downloaded when "Save recording" option is pressed | `false` | Yes |
44+
| **`downloadFileExtension`** | The file extension to be used for the downloaded file. Allowed values are `mp3`, `wav` and `webm` | `mp3` | Yes |
45+
| **`classes`** | This allows class names to be passed to modify the styles for the entire component or specific portions of it | N/A | Yes |
3846
---
3947
### **useAudioRecorder** hook
4048

4149
If you prefer to build up your own UI but take advantage of the implementation provided by this package, you can use this hook instead of the component
4250

4351
The hook returns the following:
4452

45-
#### **`startRecording`**
46-
Calling this method would result in the recording to start. Sets `isRecording` to `true`
47-
48-
49-
#### **`stopRecording`**
50-
This results in a recording in progress being stopped and the resulting audio being present in `recordingBlob`. Sets `isRecording` to `false`
51-
52-
53-
#### **`togglePauseResume`**
54-
Calling this method would pause the recording if it is currently running or resume if it is paused. Toggles the value `isPaused`
55-
56-
57-
#### **`recordingBlob`**
58-
This is the recording blob that is created after `stopRecording` has been called
59-
60-
61-
#### **`isRecording`**
62-
A boolean value that represents whether a recording is currently in progress
63-
64-
65-
#### **`isPaused`**
66-
A boolean value that represents whether a recording in progress is paused
67-
68-
69-
#### **`recordingTime`**
70-
Number of seconds that the recording has gone on. This is updated every second
53+
| Identifiers | Description |
54+
| :------------ |:---------------|
55+
| **`startRecording`** | Invoking this method starts the recording. Sets `isRecording` to `true` |
56+
| **`stopRecording`** | Invoking this method stops the recording in progress and the resulting audio is made available in `recordingBlob`. Sets `isRecording` to `false` |
57+
| **`togglePauseResume`** | Invoking this method would pause the recording if it is currently running or resume if it is paused. Toggles the value `isPaused` |
58+
| **`recordingBlob`** | This is the recording blob that is created after `stopRecording` has been called |
59+
| **`isRecording`** | A boolean value that represents whether a recording is currently in progress |
60+
| **`isPaused`** | A boolean value that represents whether a recording in progress is paused |
61+
| **`recordingTime`** | Number of seconds that the recording has gone on. This is updated every second |
7162

7263
### Sample usage of hook
7364

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-audio-voice-recorder",
33
"private": false,
4-
"version": "1.0.11",
4+
"version": "1.1.0",
55
"license": "MIT",
66
"author": "",
77
"repository": {

src/components/AudioRecordingComponent.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ import "../styles/audio-recorder.css";
1616
* @prop `onRecordingComplete` Method that gets called when save recording option is clicked
1717
* @prop `recorderControls` Externally initilize hook and pass the returned object to this param, this gives your control over the component from outside the component.
1818
* https://github.com/samhirtarif/react-audio-recorder#combine-the-useaudiorecorder-hook-and-the-audiorecorder-component
19+
* @prop `downloadOnSavePress` If set to `true` the file gets downloaded when save recording is pressed. Defaults to `false`
20+
* @prop `downloadFileExtension` File extension for the audio filed that gets downloaded. Defaults to `mp3`. Allowed values are `mp3`, `wav` and `webm`
1921
* @prop `classes` Is an object with attributes representing classes for different parts of the component
2022
*/
2123
const AudioRecorder: (props: Props) => ReactElement = ({
2224
onRecordingComplete,
2325
recorderControls,
26+
downloadOnSavePress = false,
27+
downloadFileExtension = "mp3",
2428
classes,
2529
}: Props) => {
2630
const {
@@ -33,14 +37,30 @@ const AudioRecorder: (props: Props) => ReactElement = ({
3337
recordingTime,
3438
// eslint-disable-next-line react-hooks/rules-of-hooks
3539
} = recorderControls ?? useAudioRecorder();
40+
3641
const [shouldSave, setShouldSave] = useState(false);
42+
3743
const stopAudioRecorder: (save?: boolean) => void = (
3844
save: boolean = true
3945
) => {
4046
setShouldSave(save);
4147
stopRecording();
4248
};
4349

50+
const downloadBlob = (blob: Blob): void => {
51+
const downloadBlob = new Blob([blob], {
52+
type: `audio/${downloadFileExtension}`,
53+
});
54+
const url = URL.createObjectURL(downloadBlob);
55+
56+
const a = document.createElement("a");
57+
a.style.display = "none";
58+
a.href = url;
59+
a.download = `audio.${downloadFileExtension}`;
60+
document.body.appendChild(a);
61+
a.click();
62+
};
63+
4464
useEffect(() => {
4565
if (
4666
(shouldSave || recorderControls) &&
@@ -49,7 +69,12 @@ const AudioRecorder: (props: Props) => ReactElement = ({
4969
) {
5070
onRecordingComplete(recordingBlob);
5171
}
72+
73+
if (downloadOnSavePress && recordingBlob != null) {
74+
downloadBlob(recordingBlob);
75+
}
5276
}, [recordingBlob]);
77+
5378
return (
5479
<div
5580
className={`audio-recorder ${isRecording ? "recording" : ""} ${

src/components/interfaces.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ export interface Props {
3939
* @sample_usage https://github.com/samhirtarif/react-audio-recorder#combine-the-useaudiorecorder-hook-and-the-audiorecorder-component
4040
**/
4141
recorderControls?: recorderControls;
42+
/**
43+
* If set to `true` the file gets downloaded when save recording is pressed
44+
**/
45+
downloadOnSavePress?: boolean;
46+
/**
47+
* File extension for the audio filed that gets downloaded
48+
**/
49+
downloadFileExtension?: "mp3" | "wav" | "webm";
4250
/**
4351
* Custom classes to changes styles.
4452
**/

0 commit comments

Comments
 (0)