Audio playback on android

I have created a small educational game with Hype which has background music, and then spoken audio. Both in mp3 format. The background music is played using Javascript. I have an unseen rectangle which is a persistent symbol across all scenes which has the html:

<audio id="ceol" preload="auto" loop="">
 <source src="${resourcesFolderName}/ceol.mp3" type="audio/mpeg"></audio>

I then access it via a javascript function which is triggered on userclick:

var snd2 = hypeDocument.getElementById('ceol');
snd2.volume=0.1;
snd2.play();	

This works perfect (as far as I can make out) on desktop devices, on ipad and on iphone, but the music will not play on android. Another major issue is that the ‘stop sound’ commands don't seem to be working on switching from scene to scene. I had them as actions on my ‘forward’ buttons i.e. move to next scene and then stop sound.

I have tried exporting with the API checkbox checked, and then unchecked, but neither seem to make a difference. Apologies if this issue is addressed elsewhere in the forum, but I have looked and can't find anything.

Are there different procedures required for preparing audio for content to be viewed on mobile as opposed to desktop?

If it is something obvious or basic I have omitted (or that I shouldn't have done) – maybe someone could point it out. The finished product can be found at: cailleach. The hype file is too large to upload here but can be found at cailleach.hype.pkgf - Google Drive

Can you give us a bit more information about the Android device + browser version you're using? Can you make sure your device is updated + has the latest version of its web browser?

If you can get developer tools going on your Android device ( Remote debug Android devices - Chrome for Developers ) it may give you errors and show us what's going wrong. Your code as it is looks fine.

If you are playing audio via a JavaScript function like you have above, but stop sound using Hype's built in 'stop sound' function -- this won't work. You would need to stop the sound in the context of how it was originally played. snd2.stop(); would stop the sound element you setup in the above function.

From browsing your filenames, it looks like you have mp3 files saved like ._1.mp3 with a period at the beginning. Can you rename them to be something like filename.mp3 with just a single period and see if that resolves the issue?

Thanks a million for getting back to me Daniel – you guys are always really helpful. It wasn't the filenames – as I changed them and it made no difference. I eventually played around more with it. I found that Hypes built in play sound and stop sound function didn't work for me on Android (Samsung s22 fe – Samsung Internet 23.0.0.47 – it initially wouldn't work on chrome on this device either) – sounds wouldn't be stopped and 2 audio files were playing at once. I had tried stopping sound using a javascript function. All voice files had the same class, so I just stopped them all. This worked on desktop when the javascript function was called from my ‘move to next scene’ button. It wouldn't work on mobile, I still don't know why (I know this is probably something to do with the way I have the document set up – but I can't figure out what it may be…). ANYWAY – calling the javascript function on scene unload seems to work, to stop sound.

So anyway – sound problem is now fixed.
I have noticed another problem, however – only on android, again – where a function isn't being fully executed.

I’m not going to worry about it too much now, as it was a halloween game for schools, and school in Ireland are all on holiday now – but I’ll post the code of this function, just in case there is an issue which jumps out at experts. I know the function is triggered because the first console.log appears. The second console log, however, after the line "function freagra(event)" doesn't appear, so function doesn't go further than this.


console.log("the function has started :-)");
scor++;

var correctRectangles = [
    "corcra1",
    "corcra3",
    "corcra6",
    "corcra8",
    "corcra10",
    "corcra12"
];

//var tomhasSpan = hypeDocument.getElementById("tomhas");
//tomhasSpan.innerHTML=3-scor;
//hypeDocument.getElementById("tomhas1").innerHTML=5-scor;

// Function to check if the user clicked a correct rectangle and play the appropriate timeline
function freagra(event) {
    // Get the ID of the clicked element
    var clickedID = event.target.id;
console.log(clickedID);

	if (correctRectangles.includes(clickedID)) {
        // Play the "maith_thu" timeline
        hypeDocument.startTimelineNamed("maith_thu", hypeDocument.kDirectionForward);
        return;
        console.log("maith thú");
    }    
    else if (scor==5) {
        // Play the "maith_thu" timeline
        hypeDocument.startTimelineNamed("caill", hypeDocument.kDirectionForward);
        var amline= hypeDocument.getElementById("triail");
        amline.style.display="none";
        var trup = hypeDocument.getElementById('mi_adh_caint');
		trup.volume=0;
        return;
    } 
    else {
    //scor++;
    console.log("scor:", scor); // Log the value of scor
    

	if (scor<5)
		{
        hypeDocument.startTimelineNamed("triail_aris", hypeDocument.kDirectionForward);
		}
		

    }
}

hypeDocument.getElementById("tomhas1").innerHTML=5-scor;



// Add a click event listener to all remaining rectangles
for (var i = 1; i <= 12; i++) {
    var rectangle = document.getElementById("corcra" + i);
    if (rectangle) {
        rectangle.addEventListener("click", freagra);
    }
}

(live game can be found at: NuachtAnois – the error can be found in mobile devices on the 8th scene. It happens constantly on my own phone. A colleague, however, was able to have the function executed once on his own android phone perfectly, before the problem creeped in on the second attempt at playing the game)

Again, if something really obvious jumps out at someone – it would be great to find out what it is, but if not, I’ll just store this away until next Halloween and try to figure it out then. Many thanks again Daniel.

I assume you're not getting other console logs that indicate a specific error?

I think the best way to debug this would be to add some additional logging here:

for (var i = 1; i <= 12; i++) {
    var rectangle = document.getElementById("corcra" + i);
    if (rectangle) {
        rectangle.addEventListener("click", freagra);
    }
}

Specifically I'd check the rectangle to make sure that exists.

I think there are some basic parts of this code that are potential (but not definite) issues:

  • You're using addEventListener but this code could get called a lot, so the event listeners will accumulate. Instead you may want to use the rectangle.onclick = freagra; way to assign a single handler.
  • You are getting an element by ID, but if you happen to have an element with that ID anywhere else in your document (like two elements with the same ID) it might be finding the other one
  • Your function handler freagra is the same name as the Hype function