openfl / openfl-html5

[deprecated] OpenFL HTML5 backend
Other
0 stars 1 forks source link

Sound.play doesn't loop sounds #5

Closed jgranick closed 10 years ago

jgranick commented 10 years ago

5 Issue by Meychi,

Using:

Assets.getMusic("assets/sounds/Sound.ogg").play(0, 2);

...plays the sound only once. Tried also -1.

jgranick commented 10 years ago

Comment by jgranick:

Hmm, I think its supposed to be hard-coded to always loop Assets.getMusic calls, this may be browser-dependent. Could you try ".play (9999)" or something?

jgranick commented 10 years ago

Comment by Meychi:

Ok it works on IE 10 and Firefox but not on Chrome 33. Using SoundJS for looping sounds works fine.

jgranick commented 10 years ago

Comment by jgranick:

Hmm, is there any way to know what backend SoundJS is using? For example, is it using Flash, or is it definitely using web audio, or HTML5 audio?

jgranick commented 10 years ago

Comment by Meychi:

Flash is used only if you set it up. It defaults to webaudio and fallbacks to HTML5 audio.

We have been using SoundJS in a big production over a year now and it is a pretty solid lib.

jgranick commented 10 years ago

Comment by jgranick:

So long as it is not requiring Flash, I'm open to SoundJS instead of howler.js, but perhaps this has been resolved on the repository? It looks like there may be a relevant commit:

https://github.com/goldfire/howler.js/commits/master

If you would not mind testing the latest version of howler.js (replacing howler.min.js under /templates/html) and seeing if it is resolved, I would really appreciate it!

Thanks

jgranick commented 10 years ago

Comment by Meychi:

I noticed that too. Tried the latest version but it still doesn't work. Few times heard some random noise when changing tabs but thats it.

jgranick commented 10 years ago

Comment by jgranick:

Hmm, well if SoundJS is going to give better results (and is not too large, and is not going to conflict with our other APIs/variables/etc) then it may be worth switching. Is this something you might be interested in looking into?

jgranick commented 10 years ago

Comment by Meychi:

Well i did some digging on this and found out the reason for this. With current settings (Howl.buffer=true) in Sound.hx, Howler uses HTML5 Audio with music. This causes the played music be loaded again (streamed) so you end up preloading the sound first and then not even use it. Also this brings another issue. When the HTML5 sound is played, the server responds with a partial content and Chrome doesn't like that with a proxy. :)

I tried fixing it myself by setting Howl.buffer=false but that brought another issue. For some reason then the audio sounds badly muffled/compressed on Firefox and on Chrome.

I would suggest to use SoundJS for sounds but it is triple the size of Howler (10kb vs 33kb). SoundJS is pretty mature and works pretty well on multiple HTML5 enabled devices. I think it happens to have better documentation, for example with device issues like click activated audio on iOS.

jgranick commented 10 years ago

Comment by dimanux:

Hi, I have checked this problem recently. You can add this line: https://github.com/dimanux/openfl-html5/blob/aa64238f2f3e416d248743201a9273b578e8eeca/flash/media/SoundChannel.hx#L33 to SoundChannel.hx and music will be looped. Also there are many other bugfixes.

jgranick commented 10 years ago

Comment by Meychi:

Hey thanks, great! Please make a pull request to include the fixes to the main repo.

jgranick commented 10 years ago

Comment by jgranick:

I've just included a number of minor changes here into the repository, @Meychi would you be interested in checking one more time, to see if these changes helped?

jgranick commented 10 years ago

Comment by Meychi:

Sure. These latest sound fixes made it more stable and fixed the looping but i still have the other issues. The main issue for me is that the sounds are first preloaded and then the music is streamed which makes the browser load the sounds twice.

I need to check what SoundJS is doing differently than Howler. I'll get back with my findings.

jgranick commented 10 years ago

Comment by Meychi:

Ok after some digging i was able to fix the issues. There was an reported but closed issue about this in howler github and i posted a fix to the issue: https://github.com/goldfire/howler.js/issues/112. Also the buffer property needs to be false or not set at all in Howl to not force using HTML5 Audio with music.

With these fixes, sounds don't stream and use the preloaded data and the sound is crisp. Yay!

jgranick commented 10 years ago

Comment by jgranick:

This is a hard call...

The difference between "sound" and "music" on native is that one streams, and the other is loaded completely, first. If we do not buffer music, then the user will have to download the entire MP3 (or OGG, or WAV, based on the browser) before running the title. Streaming reduces the load time a lot.

I suppose there's the trade-off, though, of looping... have you had an issues with music sounding "weird"? It seemed slow and strange yesterday while testing, but it could just be me...

jgranick commented 10 years ago

Comment by Meychi:

Well in html5 games i usually want to have everything loaded before hand as in Flash (allows better control and no stream delays) but there might be scenarios where streaming makes more sense. It seems the looping works ok with getSound too so maybe we can leave it as it is. Seems to work on latest IE & Chrome and Firefox.

I had some issues with the sound but that pannerNode fix solved most of those. Do you need to wait them to (possibly) integrate the fix or can we roll our own?

jgranick commented 10 years ago

Comment by jgranick:

I (think?) the right way to go is to leave the current behavior, perhaps you could treat the music as type "sound" when targeting HTML5, so that it preloads as you want?

I'd be very happy to include a custom update of howler.js if it improves the quality, would you mind minifying your copy?

jgranick commented 10 years ago

Comment by Meychi:

Agreed. Here is my fork, i did a pull request for it too so maybe it gets merged, who knows. :)

https://github.com/Meychi/howler.js

jgranick commented 10 years ago

Comment by jgranick:

I've just included your update.

For some reason, I'm having trouble getting HTML5 audio files to loop anymore, but strangely, reverting the howler.min.js file and reverting SoundChannel doesn't seem to make a difference, but the posted Evoland sample clearly loops. I have no idea what's up :(

jgranick commented 10 years ago

Comment by Meychi:

Thanks. All seems to work ok on my Win7 with latest IE, Chrome and Firefox. Firefox has some issues with the sound quality but i think that is due to drivers or something. I hear some crackle sometimes.

I did notice that addition to the prev tweak, SoundJS also uses dynamic compression to improve the sound quality with web audio. I do suggest that you should seriously think about using SoundJS for audio if the file size increase isn't too much.

jgranick commented 10 years ago

Comment by jgranick:

So long as it doesn't negatively impact the project (does SoundJS define Flash API globals, for example?) then I think the file size difference is negligible.

Do you know if SoundJS supports a fixed number of loops for a sound, rather than a loop Boolean? Thanks!

jgranick commented 10 years ago

Comment by Meychi:

SoundJS is part of CreateJS suite and is defined under createjs namespace.

You can set any amount of loops and -1 is for infinite playback.

jgranick commented 10 years ago

Comment by jgranick:

I've just changed to SoundJS for audio, looping appears to work fine, now. Please let me know if you notice any discrepancies with how it works, thanks again!

jgranick commented 10 years ago

Comment by Meychi:

Works great on Win7 with latest IE, Chrome and Firefox. Firefox still has little audio quality issues but i guess that is down to the webaudio implementation (or drivers) as it was the same on Howler.