Create embedded sound in source file


#1

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!


#2

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:


#3

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!


#4

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");
...

#5

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

If so, in theory my problems are solved.


#6

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!