Detailed explanation of the pitfalls encountered in HTML5 recording
This article mainly introduces the pitfall journey of explaining HTML5 recording in detail. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
To be honest, I have never been exposed to the HTML5 Audio API at the beginning, and we have to optimize it based on the code before we took over. Of course, there are also many pitfalls involved. This time I will also talk about my feelings around these pitfalls (the initialization and acquisition of some basic objects will be omitted because these contents are not the focus of this time. Interested students can search MDN by themselves. (document on):
Compatibility writing method for calling Audio API
Get the size of the recording sound (should be the frequency)
Compatibility writing method of pausing recording
Get the current recording time
Preparation before recording
Before starting recording, you must first obtain whether the current device supports Audio API. The earlier method navigator.getUserMedia has been replaced by navigator.mediaDevices.getUserMedia. Normally, most modern browsers now support the usage of navigator.mediaDevices.getUserMedia. Of course, MDN also provides compatibility instructions
const promisifiedOldGUM = function(constraints) { // First get ahold of getUserMedia, if present const getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; // Some browsers just don't implement it - return a rejected promise with an error // to keep a consistent interface if (!getUserMedia) { return Promise.reject( new Error('getUserMedia is not implemented in this browser') ); } // Otherwise, wrap the call to the old navigator.getUserMedia with a Promise return new Promise(function(resolve, reject) { getUserMedia.call(navigator, constraints, resolve, reject); }); }; // Older browsers might not implement mediaDevices at all, so we set an empty object first if (navigator.mediaDevices === undefined) { navigator.mediaDevices = {}; } // Some browsers partially implement mediaDevices. We can't just assign an object // with getUserMedia as it would overwrite existing properties. // Here, we will just add the getUserMedia property if it's missing. if (navigator.mediaDevices.getUserMedia === undefined) { navigator.mediaDevices.getUserMedia = promisifiedOldGUM; }
Because this method is asynchronous, we can Friendly reminder for incompatible devices
navigator.mediaDevices.getUserMedia(constraints).then( function(mediaStream) { // 成功 }, function(error) { // 失败 const { name } = error; let errorMessage; switch (name) { // 用户拒绝 case 'NotAllowedError': case 'PermissionDeniedError': errorMessage = '用户已禁止网页调用录音设备'; break; // 没接入录音设备 case 'NotFoundError': case 'DevicesNotFoundError': errorMessage = '录音设备未找到'; break; // 其它错误 case 'NotSupportedError': errorMessage = '不支持录音功能'; break; default: errorMessage = '录音调用错误'; window.console.log(error); } return errorMessage; } );
If everything goes well, we can move to the next step.
(The method of obtaining the context is omitted here, because this is not the focus this time)
Start recording, pause recording
Here One special point is that an intermediate variable needs to be added to identify whether recording is currently taking place. Because on the Firefox browser, we found a problem. The recording process was normal, but when we clicked to pause, we found that it could not be paused. We used the disconnect method at that time. This method is not possible. This method requires disconnecting all connections. Later it was discovered that an intermediate variable this.isRecording should be added to determine whether recording is currently taking place. When start is clicked, it is set to true and when paused, it is set to false.
When we start recording, there will be a recording listening event onaudioprocess. If true is returned, the stream will be written. If false is returned, it will not be written. Therefore, judge this.isRecording. If it is false, directly return
// 一些初始化 const audioContext = new AudioContext(); const sourceNode = audioContext.createMediaStreamSource(mediaStream); const scriptNode = audioContext.createScriptProcessor( BUFFER_SIZE, INPUT_CHANNELS_NUM, OUPUT_CHANNELS_NUM ); sourceNode.connect(this.scriptNode); scriptNode.connect(this.audioContext.destination); // 监听录音的过程 scriptNode.onaudioprocess = event => { if (!this.isRecording) return; // 判断是否正则录音 this.buffers.push(event.inputBuffer.getChannelData(0)); // 获取当前频道的数据,并写入数组 };
Of course there will be a pitfall here, that is, it can no longer be used. It comes with a method to obtain the current recording duration, because it is not actually a real pause. , but the stream is not written. So we also need to get the current recording duration, which needs to be obtained through a formula
const getDuration = () => { return (4096 * this.buffers.length) / this.audioContext.sampleRate // 4096为一个流的长度,sampleRate 为采样率 }
so that we can get the correct recording duration.
End recording
The way to end recording is to pause first, then perform listening or other operations first, and then store the array length of the stream. Set to 0.
Get frequency
getVoiceSize = analyser => { const dataArray = new Uint8Array(analyser.frequencyBinCount); analyser.getByteFrequencyData(dataArray); const data = dataArray.slice(100, 1000); const sum = data.reduce((a, b) => a + b); return sum; };
Others
- ##HTTPS: Under chrome, the entire site needs to have HTTPS before it is allowed to be used
- WeChat: The built-in browser in WeChat needs to call JSSDK to use
- Audio format conversion: There are many ways to format audio, which can be found Most of the information is basically copied from each other. Of course, there is also the issue of audio quality, which I won’t go into details here.
Conclusion
Most of the problems encountered this time were compatibility issues, so I encountered a lot of pitfalls, especially It's a problem on the mobile side. At the beginning, there was a problem with the wrong way to get the recording duration, which caused it to freeze directly. This experience has also made up for some gaps in the HTML5 API. Of course, the most important thing is to remind everyone that this kind of native API documentation is simply and crudely obtained by directly viewing MDN! Related recommendations:10 recommended articles about recording
Recording function developed by WeChat
Detailed explanation of HTML5 web page recording and compression sample code
The above is the detailed content of Detailed explanation of the pitfalls encountered in HTML5 recording. For more information, please follow other related articles on the PHP Chinese website!

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!