7

So I am having a problem where I am trying to create a web-audio source node from a tag. The Code Looks Like this:

OBJECT.musicContext= new webkitAudioContext();  
OBJECT.audio = new Audio();
OBJECT.audio.src = self.file;
OBJECT.source = OBJECT.musicContext.createMediaElementSource(OBJECT.audio);

var analyser= OBJECT.musicContext.createAnalyser();
analyser.fftSize=1024;
OBJECT.analyser=analyser    

OBJECT.gain = self.musicContext.createGain();
OBJECT.gain.gain.value = .01    
OBJECT.source.connect(OBJECT.gain)
OBJECT.gain.connect(OBJECT.analyser)
OBJECT.analyser.connect(OBJECT.musicContext.destination)

OBJECT.play = function(){OBJECT.source.play();}
OBJECT.stop = function(){OBJECT.source.stop();}

The problem is with the last two lines. I can't seem to get the audio to play through the webkit audio context...

If I do

OBJECT.play = function(){OBJECT.audio.play();}

the sound will start playing, but not through the audio node (which makes sense)

I have also tried

OBJECT.play = function(){OBJECT.source.noteOn(0);}
OBJECT.stop = function(){OBJECT.source.noteOff(0);}

to no avail...

Any help or suggestions are greatly Appreciated, and thanks in advance for your time!

Isaac

EDIT: when console.logging OBJECT.source is claims that there are zero inputs and 1 output. Is this correct for a source node?

Cabbibo
  • 1,371
  • 3
  • 17
  • 30
  • 1
    Also, you can only have **one** `AudioContext`, be sure you're not creating a new one every time. – idbehold Dec 20 '12 at 21:50
  • Only have one, made sure of that. It still seems to be the linking of the audio to the source... – Cabbibo Dec 21 '12 at 16:28

2 Answers2

1

You should try your page in Chrome as Safari currently doesn't send the correct data to the Analyzer Node. If you're using an Audio() object with the Web Audio API then you should be able to control the playback with it instead of using .noteOn()/.noteOff().

Here's the test case: http://screamingrobots.com/misc/safariaudiobug/

Community
  • 1
  • 1
idbehold
  • 16,833
  • 5
  • 47
  • 74
  • Hey! Thanks so much for the response I've been using chrome for testing, but I never knew that about safari so its great to know! I'm starting to think there may be a more fundamental problem in my code tho, because even when I control it via the – Cabbibo Dec 21 '12 at 15:55
0

Figured it out finally, but don't know exactly what the issue was...

rather then using

audio = new Audio();
audio.src = self.file;

and connecting this, I instead used

audio = document.querySelector('audio');

to select an element I had previously put into the page. I am not sure if this works simply because it gave it more time to load, or if there is something intrinsic to the API

Cabbibo
  • 1,371
  • 3
  • 17
  • 30
  • Wanted to add as well that this is what seems to work in chrome canary, however when I checked it out in Chrome it acts differently... – Cabbibo Dec 22 '12 at 21:00
  • ALSO this I think was what was the problem: crbug.com/112368 ! – Cabbibo Dec 31 '12 at 22:36