vakodiya commited on
Commit
e9fda1a
1 Parent(s): aca81e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -2
app.py CHANGED
@@ -12,13 +12,48 @@ st.title("Microphone Input in Streamlit")
12
 
13
  # Load the custom component
14
  audio_recorder_html = """
15
- <script src="mic_component.js"></script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  <button id="recordButton">Start Recording</button>
17
  <button id="stopButton">Stop Recording</button>
18
  """
19
 
20
  # Embed the JavaScript and HTML in Streamlit
21
- components.html(audio_recorder_html, height=200)
22
 
23
  # Retrieve the audio data from the component (if available)
24
  audio_data = st.experimental_get_query_params().get("value")
 
12
 
13
  # Load the custom component
14
  audio_recorder_html = """
15
+ <script>
16
+ const recordButton = document.getElementById("recordButton");
17
+ const stopButton = document.getElementById("stopButton");
18
+ const audioChunks = [];
19
+
20
+ let mediaRecorder;
21
+
22
+ navigator.mediaDevices.getUserMedia({ audio: true })
23
+ .then(function(stream) {
24
+ mediaRecorder = new MediaRecorder(stream);
25
+
26
+ recordButton.onclick = function() {
27
+ mediaRecorder.start();
28
+ console.log("Recording started...");
29
+ };
30
+
31
+ stopButton.onclick = function() {
32
+ mediaRecorder.stop();
33
+ console.log("Recording stopped...");
34
+ };
35
+
36
+ mediaRecorder.ondataavailable = function(e) {
37
+ audioChunks.push(e.data);
38
+ };
39
+
40
+ mediaRecorder.onstop = function(e) {
41
+ const audioBlob = new Blob(audioChunks, { type: "audio/wav" });
42
+ const reader = new FileReader();
43
+ reader.readAsDataURL(audioBlob);
44
+ reader.onloadend = function() {
45
+ const base64data = reader.result.split(',')[1];
46
+ Streamlit.setComponentValue(base64data);
47
+ };
48
+ };
49
+ });
50
+ </script>
51
  <button id="recordButton">Start Recording</button>
52
  <button id="stopButton">Stop Recording</button>
53
  """
54
 
55
  # Embed the JavaScript and HTML in Streamlit
56
+ components.html(audio_recorder_html, height=300)
57
 
58
  # Retrieve the audio data from the component (if available)
59
  audio_data = st.experimental_get_query_params().get("value")