Created
August 6, 2024 05:16
-
-
Save gigincg/68612d357bc4655f03389eb23f61aa2c to your computer and use it in GitHub Desktop.
Medispeak Test Page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <title>Audio Upload</title> | |
| </head> | |
| <body> | |
| <button id="start-rec">Start Recording</button> | |
| <button id="stop-rec" disabled>Stop Recording</button> | |
| <button id="upload-rec" disabled>Upload Recording</button> | |
| <script> | |
| document | |
| .getElementById("start-rec") | |
| .addEventListener("click", startRecording); | |
| document | |
| .getElementById("stop-rec") | |
| .addEventListener("click", stopRecording); | |
| document | |
| .getElementById("upload-rec") | |
| .addEventListener("click", uploadRecording); | |
| let mediaRecorder; | |
| let audioChunks = []; | |
| function startRecording() { | |
| navigator.mediaDevices | |
| .getUserMedia({ audio: true }) | |
| .then((stream) => { | |
| mediaRecorder = new MediaRecorder(stream); | |
| mediaRecorder.start(); | |
| mediaRecorder.addEventListener("dataavailable", (event) => { | |
| audioChunks.push(event.data); | |
| }); | |
| mediaRecorder.addEventListener("stop", () => { | |
| const audioBlob = new Blob(audioChunks, { type: "audio/wav" }); | |
| const audioUrl = URL.createObjectURL(audioBlob); | |
| const audio = new Audio(audioUrl); | |
| audio.play(); // Optional: Play the recording for the user | |
| window.recordingBlob = audioBlob; // Save the blob for uploading | |
| document.getElementById("upload-rec").disabled = false; | |
| }); | |
| document.getElementById("stop-rec").disabled = false; | |
| }) | |
| .catch((error) => | |
| console.error("Error accessing media devices.", error) | |
| ); | |
| } | |
| function stopRecording() { | |
| mediaRecorder.stop(); | |
| document.getElementById("stop-rec").disabled = true; | |
| } | |
| function uploadRecording() { | |
| const formData = new FormData(); | |
| formData.append( | |
| "transcription[audio_file]", | |
| window.recordingBlob, | |
| "transcript.wav" | |
| ); | |
| const pageId = "your_page_id_here"; // You need to set this appropriately | |
| fetch( | |
| `https://app.medispeak.in/api/v1/pages/${pageId}/transcriptions`, | |
| { | |
| method: "POST", | |
| headers: { | |
| Authorization: `Bearer ${sessionStorage.getItem("access_token")}`, | |
| }, | |
| body: formData, | |
| } | |
| ) | |
| .then((response) => response.json()) | |
| .then((data) => { | |
| console.log("Upload successful:", data); | |
| }) | |
| .catch((error) => { | |
| console.error("Error uploading file:", error); | |
| }); | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment