Skip to content

Instantly share code, notes, and snippets.

@ashraf2047
Last active August 16, 2022 04:16
Show Gist options
  • Select an option

  • Save ashraf2047/d9fc6639cbb1ba1e05e041ca59fac125 to your computer and use it in GitHub Desktop.

Select an option

Save ashraf2047/d9fc6639cbb1ba1e05e041ca59fac125 to your computer and use it in GitHub Desktop.
initRecorder = async () => {
navigator.getUserMedia =
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
if (navigator.mediaDevices) {
const s = await navigator.mediaDevices.getUserMedia({
audio: true,
});
this.mediaRecorder = new MediaRecorder(s);
this.chunks = [];
this.mediaRecorder.ondataavailable = (e) => {
if (e.data && e.data.size > 0) {
this.chunks.push(e.data);
}
};
this.stream = s;
this.mediaRecorder.addEventListener("stop", () => {
this.setStream(null);
});
}
};
export const uploadAudioToS3 = async (
postId,
userId,
blob,
redirect = false
) => {
s3.upload(
{
Bucket: "test",
Body: blob,
Key: `${userId}/${postId}.mp3`,
ContentType: "audio/mp3",
},
function (err, data) {
if (err) {
console.log(err);
} else if (redirect) {
// reload and redirect to explore page.
window.location.href = "/explore";
}
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment