openfl / openfl

The Open Flash Library for creative expression on the web, desktop, mobile and consoles.

Home Page:http://www.openfl.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[HTML5] Unable load the raw bytes of a sound file.

UncertainProd opened this issue · comments

Describe the bug
I've been looking for a way of getting the raw bytes of an mp3 sound file (not as a Sound object) that's in the assets directory (under assets/sounds/), but when I try to use Assets.loadBytes('assets/sounds/sound.mp3') it gives me a 404 error in html5. However, when I load the sound file directly using Assets.loadSound, I do successfully get an openfl.media.Sound object. Additionally, using Assets.loadBytes with the mp3 file on windows does work without errors.

To Reproduce
Steps to reproduce the behavior:
Example Code snippet:
This code will result in a 404 error on html5:

Assets.loadBytes('assets/sounds/sound.mp3').onComplete((mp3Bytes) ->
    {
	    trace('Loaded bytes. Is null? ${mp3Bytes == null}');
            // some processing on the bytes...
    }).onProgress((a, b) ->
    {
        trace('Progress: $a | $b');
    }).onError((err) ->
    {
        trace('ERROR: $err'); // Shows error 404 on html5
    });

However, the following works on both html5 and desktop:

Assets.loadSound('assets/sounds/sound.mp3').onComplete((soundObj) ->
{
    soundObj.play(); // works
});

Expected behavior
The onComplete callback should be called with bytes of the mp3 file

OpenFL Targets
I've only seen this problem in html5. This issue does not happen on windows as far as I've checked

Additional context

Does it work if you add type="binary" to the <assets> element in project.xml?

<assets path="assets/sounds/sound.mp3" embed="false" type="binary"/>

You may not be able to load an asset as a different type.

Yes, that seems to work, thanks!
By the way, what does the embed field do? Does it just decide if the asset is cached?

It controls whether assets are preloaded by the app on startup or if you need to ask them to be loaded on demand. By default, I believe that embed is true. When assets are embedded, you can use APIs like Assets.getBytes() or Assets.getSound() and it will return the asset immediately, without needing an onComplete callback. If you use embed="false", you need to ask to load each assets, and wait until it has completed loading, before you can use it. Assets that are not embedded must use Assets.loadBytes() or Assets.loadSound() with onComplete callbacks.

Since you were using Assets.loadBytes() and Assets.loadSound(), I assumed that you had embed="false".

Oh okay. Thanks for the info!