Sound stopping in iOS

Hi, I’ve noticed that running a lime app in iOS will stop any sound from playing, for example if you have spotify playing a track and then open a lime app it will immediately fade the song out and stop it. Is there a way to allow the song to play?

1 Like

I’ve noticed this too. I don’t think that there’s a way to change this behavior in Lime as it is currently implemented.

I haven’t tested to be sure, but I suspect that we wouuld need to expose a way to configure SDL_HINT_AUDIO_CATEGORY in Haxe (or maybe in project.xml), and that would get passed to Lime’s C++ code to call something like this:

SDL_SetHint (SDL_HINT_AUDIO_CATEGORY, "AVAudioSessionCategoryAmbient");

Thanks, i will try looking to see if I can set this and let you know how I get on.

So I tried putting:

SDL_SetHint (SDL_HINT_AUDIO_CATEGORY, “ambient”);

Into SDLApplication.cpp, but it did not work unfortunately, it still cuts music off every time you enter the app. I tried several different places and variations, but nothing worked.ChatGPT suggested that maybe it doesn’t work because the sound is handled by OpenAL, I don’t know though.

I did then put the following code into a native extension:

void NativeFunctions::enableambient() 
{
	AVAudioSession *session = [AVAudioSession sharedInstance];
	NSError *err = nil;

	if (![session setCategory:AVAudioSessionCategoryAmbient
			withOptions:AVAudioSessionCategoryOptionMixWithOthers
			error:&err]) {
		[session setActive:NO
     			withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
        	   error:&err];
	}
}

This does work - if you switch to the app it wont turn off background music, but annoyingly it will still stop any music when the app starts. I guess maybe I’m not calling it early enough?

I could try and call it from didFinishLaunchingWithOptions, but I’m not sure if there’s a good way of calling functions from there without overwriting the main.mm file.

So I was looking at the SDL source code, and I don’t even see a comparison for the string “ambient”, even though that’s what is documented. I did see a comparison for “AVAudioSessionCategoryAmbient”, though. So if you didn’t try that, it might be worth a shot.

Also, in case you weren’t aware, after changing Lime’s C++ Code, you always need to run lime rebuild ios (or whichever native target you are using, like windows, android, mac, or linux) to get a new library binary.

So I tried:
SDL_SetHint (SDL_HINT_AUDIO_CATEGORY, “AVAudioSessionCategoryAmbient”);
as you suggested, but still no luck. I’m rebuilding lime for iOS in between each time. This probably isn’t a massive priority or anything, but still nice to have the option, especially for certain apps.

I see in NativeApplication.hx, we call AudioManager.init() before NativeCFFI.lime_application_create() where we create the SDLApplication instance. I suspect that that AVAudioSessionCategoryAmbient needs to be set before initializing the audio.