How to record audio from microphone by nicegui? #569
-
I didn't find a component for audio recording. Is this feature temporarily unavailable? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
No, there is no NiceGUI element or function to record audio at the moment. |
Beta Was this translation helpful? Give feedback.
-
Here is a proof-of-concept for recording audio with NiceGUI. It records 3 seconds and plays it back afterwards: async def record():
await ui.run_javascript('''
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
const mediaRecorder = new MediaRecorder(stream);
mediaRecorder.start();
const audioChunks = [];
mediaRecorder.addEventListener("dataavailable", event => audioChunks.push(event.data));
mediaRecorder.addEventListener("stop", () => {
const audioBlob = new Blob(audioChunks);
const audioUrl = URL.createObjectURL(audioBlob);
const audio = new Audio(audioUrl);
audio.play();
});
setTimeout(() => mediaRecorder.stop(), 3000);
});
''', respond=False)
ui.button('Record').on('click', record) Of course the code might get more involved depending on what you want to do with the audio. |
Beta Was this translation helpful? Give feedback.
-
Unfortunately, your solution @falkoschindler does not seem to work anymore. Besides that
Python Version: Python 3.11.6 |
Beta Was this translation helpful? Give feedback.
Here is a proof-of-concept for recording audio with NiceGUI. It records 3 seconds and plays it back afterwards: