Create embedded sound in source file

Hi, i am trying to use sounds embedded directly in the source files, as in this example:

@:keep @:sound("assets/sounds/mp3/click.mp3")
private class Logo extends openfl.media.Sound {}

How would i go about creating the Sound instance itself?

Thanks!

On Flash, try var sound = new Logo (); sound.play ();, on native, try OGG, on HTML5, I think embedded sounds won’t work properly :slight_smile:

Hmmm, i need it specifically for HTML5. Is there any other way to bypass loading to be able to run it locally (without the CORS policy error)?

I know i can add them to index.html and they’ll run fine, but how do i access “audio” tags from OpenFL?

Thanks!

You could try doing something like this at runtime, but it may still be the same CORS issue you’re experiencing:

Sound.loadFromFiles ([ "path/to/sound.ogg", "path/to/sound.mp3", "path/to/sound.wav" ]).onComplete (function (sound) { ... });

The alternative would be to use something such as:

var audio = js.Browser.document.body.getElementById ("audio-tag");
...

Can i cast or convert such an element to openfl.media.Sound ?

If so, in theory my problems are solved.

I solved my problem with

var audio = cast(js.Browser.document.body.getElementById ("audio-tag"), js.html.MediaElement);
audio.play();

Thanks for the help Josh!

1 Like