3

I am trying to analyse a live audio stream from a url.

I have tested my code with mic input and it works well however when I change

createMediaStreamSource(micStream)

To

var streamURL = new Audio(['http://urlOnRemoteServer'])
createMediaStreamSource(streamURL)

Then getByteFrequencyData just returns arrays of zeros (with mic input it returns real data instead of zeros).

It looks like firefox has a Same-origin policy so I moved my code to a web server and used a temporary audio file, however this produces the same results on both Firefox and Chrome so I assume this isn't to do with browser support...

Example JSFiddle: https://jsfiddle.net/j68fhz9x/

I am using Firefox/Chrome on Debian Linux

Thanks in advance

UPDATE

After looking into CORS I instead tried to run the app locally, however the problem persists even if I have both the script and audio file on a local computer.

Slidon
  • 393
  • 2
  • 3
  • 16
  • What happens when you connect the source to the master output? Do you hear any sound? – reg4in Jun 21 '15 at 10:31
  • Also, could you provide a fiddle or similar? – reg4in Jun 21 '15 at 10:32
  • Fiddle now included in answer – Slidon Jun 21 '15 at 10:53
  • So in my enviroment (Manjaro Linux/FF 38), when I open the linked audio stream directly, it does not work either. You should try with a test file. – reg4in Jun 21 '15 at 10:57
  • Yes the stream seems to be glitching a bit at the moment, I have replaced it with a test file – Slidon Jun 21 '15 at 11:07
  • If by running locally means you load the files through file:// then this is also considered being different origin. You need to set up a server (or use an IDE like WebStorm, VIsual Studio with a built-in server) and run from localhost/127.0.0.1 –  Jun 24 '15 at 13:38

1 Answers1

3

You need to use a same origin file or CORS, for Firefox and chrome.

cwilso
  • 13,610
  • 1
  • 30
  • 35
  • I really wish browser vendors would do the same the same thing they do with XHRs and output some error/warning to the console so at least the developer has some hint as to why their audio is silent and how to fix it. – idbehold Jun 21 '15 at 13:35
  • 1
    Well, at least in Chrome 45 it now outputs the following to the console when you run into this issue now: "MediaElementAudioSource outputs zeroes due to CORS access restrictions for `AUDIO_URL`" However Firefox 40 still provides nothing helpful in the console. – idbehold Jun 21 '15 at 15:29
  • I have had a look at making CORS requests but how can I make one if the url is passed as a string parameter? eg new Audio(["cors_url_request"]) – Slidon Jun 21 '15 at 21:35
  • Try streamURL.crossOrigin = 'anonymous' – Raymond Toy Jun 24 '15 at 05:40