search
HomeWeb Front-endH5 TutorialResource recommendations for HTML5 music visualization video tutorials

Is the simple music playback too monotonous? If you can also see the music while listening to it, it will be more interesting. This course will lead you to use webAudio and canvas to visualize your music in the form you like and make your music move.

Resource recommendations for HTML5 music visualization video tutorials

Course playback address: http://www.php.cn/course/327.html

The Teacher's teaching style:

The teacher's lectures are simple and in-depth, clear in structure, analyzed layer by layer, interlocking, rigorous in argumentation, and rigorous in structure. He uses the logical power of thinking to attract students' attention and controls the classroom with reason. teaching process. By listening to the teacher's lectures, students not only learn knowledge, but also receive thinking training, and are also influenced and influenced by the teacher's rigorous academic attitude

The more difficult point in this video is HTML5 music visualization:

Music acquisition and playback

Building the front-end and back-end of the application

1, create a new media data folder, public/media, put the audio data into it
2, build the page CSS framework, /public/stylesheets/index.css
3, read the page content, views/index.ejs
4, background routing Control, routes/index.js, get the music list and return it to the previous section

ajax request server-side audio data

Create a new file index.js under javascripts, reference and create it in views/index.ejs File

<script type="text/javascript" src="/javascripts/index.js"></script>

Edit and create files to achieve click effects

<ul id="list">
        <% music.forEach(function(name){ %>
              <li title="<%= name %>"><%= name %></li>    #设置title属性
        <% }) %>
</ul>


Decode and play audio

AudioContext

An object containing each AudioNode object and their connections, that is, the audio context object. There is only one AudioContext created in a document: var ac = new window.AudioContext();

Attributes:

destination, AudioDestinationNode object, where all audio outputs gather, equivalent to audio hardware, All AudioNodes are directly or indirectly connected to here.

currentTime, the time (seconds) from the creation of AudioContext to the current time.

Method:

decodeAudioData(arrayBuffer,succ(buffer),err), asynchronously decode audio data contained in arrayBuffer

createBufferSource(), create autoBufferSourceNode object

createAnalyser(), create AnalyserNode object

createGain()/createGainNode(), create GainNode object

AudioBufferSourceNode

represents an audio resource in memory, its audio Data is stored in AudioBuffer (its buffer attribute)
Creation: var buffersource = ac.createBufferSource();

Properties:

buffer, AudioBuffer object, represents the audio resource data to be played
——Sub-attribute: duration, the duration of the audio resource (seconds)

loop, whether to loop playback, default false

onended, can be bound to the time called when the audio playback is completed Handler

Method:

start/noteOn(when=ac.currentTime,offset=0,buration=buffer.duration-offset), start playing audio.
when: when to start playing;
offset: how many seconds to start playing the audio;
duration: how many seconds to play

stop/noteOff(when=ac.currentTime), end Play audio

Add volume control

GainNode

An object that changes the audio volume and changes the signal strength of all sampleframes passing its audio data
Create: var gainNode = ac.createGain()/ac.createGainNode();

gain, AudioParam object, the intensity of the audio signal can be changed by changing its value. The default value attribute value is 1, and the minimum value is 0. The maximum value is 1, and its value can also be greater than 1 and less than 0

Playback bug fix

Problem: When playing the second song, the first song is still playing. The main reason is Each time you click on the music list, load("/media/"+this.title) is called, the data is decoded and played:

xhr.onload = function(){
    ac.decodeAudioData(xhr.response, function(buffer){
        var bufferSource = ac.createBufferSource();
        bufferSource.buffer = buffer;
        bufferSource.connect(gainNode);
        bufferSource[bufferSource.start?"start":"noteOn"](0);
    }, function(err){
                console.log(err);
    });
}

Solution:
Assign a null value to the audio data var source = null;, Save the decoded data of the previous song source = bufferSource;, and judge the execution to stop playing source && source[source.stop? "stop" : "noteoff"](0);

Music data visualization

AnalyserNode

音频分析对象,它能实时的分析音频资源的频域和时域信息,但不会对音频流做任何处理
创建:var analyser = ac.createAnalyser();

fftSize,设置FFT(FFT是离散傅里叶变换的快速算法,用于将一个信号变换到频域)值得大小,用于分析得到频域,为32 - 2048之间2的整数倍,默认为2048。实时得到的音频频域的数据个数为FFTSize的一半

frequencyBinCount,FFT值得一半,即实时得到的音频频域的数据个数

getByteFrequencyData(Uint8Array),复制音频当前的频域数据(数量是FrequencyBinCount)到Uint8Array(8位无符号整型类型化数组)中

创建Analyser对象:

var analyser = ac.createAnalyser();
analyser.fftSize = 512;
analyser.connect(gainNode);

连接到分

析对象获取数据:bufferSource.connect(analyser);

实现可视化功能函数:

function visualizer(){
    var arr = new Uint8Array(analyser.frequencyBinCount);
    analyser.getByteFrequencyData(arr);
    console.log(arr);
}

调用visualizer函数:

利用canvas将音乐数据可视化(柱状图)

在views下加入id

控制高度变化:

var box = $("#box")[0];
var height, width;
var canvas = document.createElement("canvas");
box.appendChild(canvas);
 
function resize(){
    height = box.clientHeight;
    width = box.clientWidth;
    canvas.height = height;
    canvas.width = width;
}
resize();    #调用触发函数
 
window.onresize = resize;

利用canvas将音乐数据可视化(圆点图)

应用优化

webAudio API

webAudio核心功能封装为对象

The above is the detailed content of Resource recommendations for HTML5 music visualization video tutorials. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML5 and H5: Understanding the Common UsageHTML5 and H5: Understanding the Common UsageApr 22, 2025 am 12:01 AM

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5: The Building Blocks of the Modern Web (H5)HTML5: The Building Blocks of the Modern Web (H5)Apr 21, 2025 am 12:05 AM

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

H5 Code: Writing Clean and Efficient HTML5H5 Code: Writing Clean and Efficient HTML5Apr 20, 2025 am 12:06 AM

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.

H5: How It Enhances User Experience on the WebH5: How It Enhances User Experience on the WebApr 19, 2025 am 12:08 AM

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

Deconstructing H5 Code: Tags, Elements, and AttributesDeconstructing H5 Code: Tags, Elements, and AttributesApr 18, 2025 am 12:06 AM

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

Understanding H5 Code: The Fundamentals of HTML5Understanding H5 Code: The Fundamentals of HTML5Apr 17, 2025 am 12:08 AM

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

H5 Code: Best Practices for Web DevelopersH5 Code: Best Practices for Web DevelopersApr 16, 2025 am 12:14 AM

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.

H5: The Evolution of Web Standards and TechnologiesH5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

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