Music loop plays on macOS app after quitting app

Hello, I'm about to submit my AlphaPod app to Apple's Mac App Store, but there's a problem with the sound of the app. I have an audio loop that I load in Hype via the simple onClick play sound X and I have it as a loop.

But when I open the build on my Mac, play the button that starts the music, and then quit the App the music loops keeps playing for about 20 seconds. Then is stops and I get this message in the console:

I've tried "Application does not run in the background" in the info.plists but the problem persists.

Also, I do not even have Background Modes setup under my target Capabilites, so it's not like I have Audio playing in the background turned on

Same Hype file quits fine on browser, and on iOS devices, it's just as a Mac App that this issue
comes up.

Thank you in advance!

That's really interesting that it happens on quit with the Mac. Is this a Catalyst-based app? (Well, if so it is less surprising since I've heard Catalyst is pretty buggy!)

If so, maybe some of the two iOS-related threads could apply:

It sees a solution would be to handle the visibilitychange event and if the document is hidden to run code which pauses the audio. A head html solution would look something like:

<script>

document.addEventListener("visibilitychange", (function () {
	if (document['hidden']) {
		var hypeDocument = Object.values(HYPE.documents)[0]; // get first hypeDocument
		hypeDocument.triggerCustomBehavior("muteAll"); // do something that mutes audio in the Hype document; here I would have setup a custom behavior that sets all of them to pause
	}
}));

</script>

Are you able to take this type of approach?

Hey Jonathan,

I tried your code on the head of the HTML, and added a "muteAll" trigger that stops the looping music file, but sadly it didn't work.

I do use Mac Catalyst which as you say, it's pretty buggy. I"m going to go through the posts you mentioned see if anything there helps.

Thanks again!

Assuming the muteAll trigger does work as expected, perhaps this event doesn't fire on quit. Instead you might want to try something like from this tip:

Basically it uses the native app's NSNotificationCenter to listen to an event and calls into javascript from there. I suspect you'd want to change the event to UIApplicationWillTerminate or use the delegate:

https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate?language=objc

And then just use the javascript from the handler above.

At least that's my best guess :slight_smile:.

It's also be interesting to look in Activity Monitor and see what is going on/if the WKWebView process is sticking around. I'm not sure how the lifecycle for Catalyst apps is different than normal Mac apps, but it really should go away immediately when the app is quit!

Solved!

Apparently WebKit was adding processes from Hype engine to the background thread, so it was preventing the app from quitting. So I added a rough termination of the app on quit actions.

NotificationCenter.default.addObserver(self,
selector: #selector(terminateApp),
name: .appTerminate,
object: nil)

2 Likes