gpt4 book ai didi

javascript - 读取本地声音文件并在html5 canvas中获取波形

转载 作者:行者123 更新时间:2023-11-28 07:37:35 25 4
gpt4 key购买 nike

我目前正在尝试从用户给出的声音文件中获取波形。现在从服务器加载的默认歌曲将正确播放和显示,但是当用户选择声音文件时,它将播放声音而不是波形。

// Create a new instance of an audio object and adjust some of its properties
var audio = new Audio();
audio.src = 'song.mp3';
audio.controls = true;
audio.loop = false;
audio.autoplay = true;
// Establish all variables that your Analyser will use
var source, context, analyser, fbc_array, bars, bar_x, bar_width, bar_height;



function initMp3Player(){
document.getElementById('audio_box').appendChild(audio);
context = new webkitAudioContext(); // AudioContext object instance
analyser = context.createAnalyser(); // AnalyserNode method
// Re-route audio playback into the processing graph of the AudioContext
source = context.createMediaElementSource(audio);
source.connect(analyser);
analyser.connect(context.destination);
}

function init() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

ctx.fillStyle = "rgb(0,0,0)";
ctx.fillRect(0, 0, canvas.width, canvas.height);

//add load file ability
document.getElementById('audio_file').onchange = function(){
audio = new Audio();
var files = this.files;
var file = URL.createObjectURL(files[0]);
audio.src = file;
initMp3Player();
};

initMp3Player();
}

最佳答案

我不应该再次调用 initMp3Player()。

关于javascript - 读取本地声音文件并在html5 canvas中获取波形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28420190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com