Sound does not play in HTML5

Been trying to get sounds to work on HTML5 target. Sound works in neko and mac targets but not in HTML5

project:

import openfl.media.Sound;
import openfl.media.SoundChannel;

introSound = Assets.getSound(“assets/sound/intro.ogg”);
var channel:SoundChannel = introSound.play();

What am I doing wrong?

1 Like

Browser console outputs the following:

[Log] [lime.Assets] Audio asset "assets/sound/intro.ogg" exists, but only asynchronously (Devmatg.js, line 24963)
[Log] AssetManager.hx:1191: [AssetManager] Adding sound 'intro' (Devmatg.js, line 9284)

getting this error:

TypeError: null is not an object (evaluating 'this.introSound.play')

Really? Am I the only who is experiencing audio problems on HTML5? Spent most of the day yesterday trying to figure this out but I am still unable to figure out why audio only works on neko, mac, windows, but not html.

Maybe someone else has more, or better info, but I’ll give you what I know.

If it’s a music sound, you can only play one at a time. If it’s a sound sound, you can play many. You can use project.xml to set them up.

This is what I do for cross-platform:

In project.xml

<assets path="assets" include="*" />
<assets path="assets" if="android" exclude="*.mp3" />
<assets path="assets" if="html5" exclude="*.mp3" />
<assets path="assets" if="flash" exclude="*.ogg" />
<assets path="assets" if="windows" exclude="*.mp3" />

<assets path="assets/sounds" rename="sounds" unless="flash">
	<sound path="sound1.ogg" id="sound1" />
	<sound path="sound2.ogg" id="sound2" />
	<sound path="sound3.ogg" id="sound3" />
</assets>
<assets path="assets/music" rename="music"  unless="flash">
	<music path="intro.ogg"  id="intro" />
	<music path="music1.ogg" id="music1" />
	<music path="music2.ogg" id="music2" />
	<music path="music3.ogg" id="music3" />
</assets>

<assets path="assets/sounds" rename="sounds" if="flash">
	<sound path="sound1.mp3" id="sound1" />
	<sound path="sound2.mp3" id="sound2" />
	<sound path="sound3.mp3" id="sound3" />
</assets>
<assets path="assets/music" rename="music"  if="flash">
	<music path="intro.mp3"  id="intro" />
	<music path="music1.mp3" id="music1" />
	<music path="music2.mp3" id="music2" />
	<music path="music3.mp3" id="music3" />
</assets>

Then use the id to get the sound, no matter which platform you target.

introSound = Assets.getSound("intro");

I think you will have problems with html5 if you have any mp3 files in your assets and don’t exclude them in project.xml, if I remember. If you do have any mp3s in your assets folder, adding <assets path="assets" if="html5" exclude="*.mp3" /> to `project.xml’ might be all you need.

Hope it helps. Maybe someone with more knowledge can help.

Not using mp3 and I have no plans to target flash. I am only playing one .ogg file at a time. When I check the browser console it says the file is not being loaded. It may be an error on my part but I have not been able to figure out what it is. I have followed different tutorials but all pretty much are the same to an extent.

I forgot to mention I am working on a Mac.

What does your project.xml look like? Do the PiratePig and PlayingSound samples work for you?

I figured out my problem. I started using starling and it’s asset manager and now I can successfully load a sound and play it on an HTML5 target. However I innitially encoutered a similar problem with the sound not loaded. The reason my intro.ogg was seemed to be playing was because that was not the sound that was playing at all. I forgot I had also generated an mp3 version of it and that is the file that was actually playing. Once I generated mp3’s for the other files the sound loaded and played successfully.

As of this writing I have tested simply loading openFL asset manager without the starling extended version but I suspet it is the same thing. Since I was able to play the sound with openFL’s Asset class before.