最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML 5 Audio - AnalyserNode.getByteFrequencyData() Returns Undefined - Stack Overflow

programmeradmin4浏览0评论

This appears to be a mon question - Javascript Web Audio API AnalyserNode Not Working - But I can't be sure if I've found an edge case with my implementation.

I create an audio source using createMediaElementSource(), but instead of using an audio tag from the page markup, the element is created dynamically using Buzz.js.

Here's my test setup:

window.addEventListener('load', function(e) {
    audioContext = new webkitAudioContext();
    audioAnalyser = audioContext.createAnalyser();

    sound = new buzz.sound("sound.mp3");
    sound.load().bind("canplaythrough", function(e) {
        source = audioContext.createMediaElementSource(this.sound);
        source.connect(audioAnalyser);

        audioAnalyser.connect(audioContext.destination);

        this.play().loop(); 
    });
}, false);

window.setInterval(function(){
    array = new Uint8Array(audioAnalyser.frequencyBinCount);
    console.log(audioAnalyser.getByteFrequencyData(array));                                           
})    

With the code above, the sound plays. I can also attach others nodes (biquad filters, gain etc) which work, but the audioAnalyser.getByteFrequencyData returns undefined values on every frame...

Might it have anything to do with using Buzz.js?

This appears to be a mon question - Javascript Web Audio API AnalyserNode Not Working - But I can't be sure if I've found an edge case with my implementation.

I create an audio source using createMediaElementSource(), but instead of using an audio tag from the page markup, the element is created dynamically using Buzz.js.

Here's my test setup:

window.addEventListener('load', function(e) {
    audioContext = new webkitAudioContext();
    audioAnalyser = audioContext.createAnalyser();

    sound = new buzz.sound("sound.mp3");
    sound.load().bind("canplaythrough", function(e) {
        source = audioContext.createMediaElementSource(this.sound);
        source.connect(audioAnalyser);

        audioAnalyser.connect(audioContext.destination);

        this.play().loop(); 
    });
}, false);

window.setInterval(function(){
    array = new Uint8Array(audioAnalyser.frequencyBinCount);
    console.log(audioAnalyser.getByteFrequencyData(array));                                           
})    

With the code above, the sound plays. I can also attach others nodes (biquad filters, gain etc) which work, but the audioAnalyser.getByteFrequencyData returns undefined values on every frame...

Might it have anything to do with using Buzz.js?

Share Improve this question edited May 23, 2017 at 10:28 CommunityBot 11 silver badge asked Nov 16, 2013 at 13:56 CharlieCharlie 4,3296 gold badges45 silver badges60 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

It was a failure of prehension...

Basically, I was expecting the console to log some kind of output from the following:

window.setInterval(
    array = new Uint8Array(audioAnalyser.frequencyBinCount);
    console.log(audioAnalyser.getByteFrequencyData(array));                                           
)

What I should have done was this:

window.setInterval(function(){
    array = new Uint8Array(audioAnalyser.frequencyBinCount);
    audioAnalyser.getByteFrequencyData(array); 
    console.log(array);                                 
})

A bit silly, but I hope that helps anyone ing here who might have expected getByteFrequencyData to return a value of somekind.

发布评论

评论列表(0)

  1. 暂无评论