SDL_mixer - sound is delayed?

hi.
when playing sound using sdl mixer it seems like there is a delay with playing the sound and id doesn’t happen instantly, specially on android device, is there a way to fix it?

Audio::Audio()
{
	intance = this;

	if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
	{
		printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
	}

	Mix_AllocateChannels(32);
}

void Audio::PlaySound(std::string key)
{
    int chnl = Mix_PlayChannel(-1, sounds[key], 0);
}

void Audio::LoadSound(std::string path)
{
    sounds[path] = nullptr;
    Mix_Chunk* sound = Mix_LoadWAV(path.c_str());

    if (sound == NULL)
    {
    	printf("Failed to load sound SDL_mixer Error: %s\n", Mix_GetError());
    }
    else
    {
    	sounds[path] = sound;
    }
}

See this thread.

Since you’re targeting Android, also refer to this for what to expect,
https://juce.com/discover/stories/Mobile%20performance%20index%20

When you look at the list you’ll notice targetable audio latency without glitchiness isn’t great on a lot of devices, specifically Android. Apple fares a lot better; probably because they test and optimize for it. For lot of other vendors, it varies.

Also add this line to your AndroidManifest.xml

<uses-feature android:name="android.hardware.audio.low_latency" android:required="false" />

3 Likes

More useful stuff here.