Home >Web Front-end >JS Tutorial >How to Record Audio Using the MediaStream API
This tutorial demonstrates building a web application for audio recording and server-side storage using the Media Capture and Streams API (MediaStream API). Users record audio, save it to the server, and view/play saved recordings. The server-side is implemented with Node.js and Express, while the client-side uses HTML, CSS (Bootstrap), and JavaScript.
Key Features:
Server Setup (Node.js & Express):
npm init -y
), and install dependencies (npm i express nodemon multer
).uploads
directory with timestamps for unique filenames./record
(POST) for uploading recordings and /recordings
(GET) for retrieving a list of saved recordings.npm start
.Recording Page (Client-Side):
public/index.html
) is created with a record button, an audio player container, and a container to display saved recordings. Bootstrap is used for styling.public/assets/css/index.css
): CSS styles the interface elements.public/assets/js/record.js
): JavaScript handles user interaction, recording using getUserMedia
, and uploading using the Fetch API. Event listeners manage recording start/stop, saving, discarding, and playback.Workflow:
/record
endpoint./recordings
endpoint retrieves a list of saved files for display on the page.Conclusion:
This tutorial provides a comprehensive guide to building a functional audio recording application. The use of the MediaStream API and a well-structured server-side component ensures a robust and user-friendly experience. Further enhancements could include more advanced features such as file validation, user authentication, and improved UI elements.
FAQs about the MediaStream API:
getUserMedia
to access the camera.The above is the detailed content of How to Record Audio Using the MediaStream API. For more information, please follow other related articles on the PHP Chinese website!