How to play sound in a Chrome app?

Asked

Viewed 645 times

1

I have an application for Google Chrome and I want it to emit a sound every time a notification is created, but in my tests I was not very successful =p

I don’t know if it’s some permission or if there really isn’t any way I can do it.

Do you know any way? (Detail: I wanted the js file in the background to do this)

  • What did you try? I imagine injecting an element <audio> via js should work.

  • i tried to use an audio tag and js: new audio(''). play; none of the 2 worked =/

  • And what format does the audio file have? I think it needs to be ogg.

  • Hm, I think I made a mistake, check out the format table: https://developer.mozilla.org/en-US/docs/HTML/Supported_media_formats

  • this link has no table =p

  • 1

    There at the end has a compatibility table, by file format and browser.

  • Wow, that weird, now it worked =O Thanks o/

  • 1

    Cool, I was already going to do a test, saved me work :) Now I’ll give you work: can you post the solution as a response to help future visitors? Thank you!

Show 3 more comments

1 answer

1

Already tried using the Web Audio API?

 var context = new webkitAudioContext();
 var oscillator = context.createOscillator();
 oscillator.connect(context.destination);
 oscillator.frequency.value = 780; //frequencia do som, altere pra mudar o som que será emitido
 oscillator.start(0);
 oscillator.stop(context.currentTime + 0.5)//duração do som;

If you use the API take into account that the "Oscillator" is destroyed after use, for each sound you must create a new.

Cross-browser: http://caniuse.com/#feat=audio-api

Example taken from: http://pt.slideshare.net/eshiota/retrojs-escrevendo-msicas-da-era-8bits-com-javascript-e-web-audio-api

  • I’ll try Luke :) It’s kind of hard to find a good note =O

Browser other questions tagged

You are not signed in. Login or sign up in order to post.