UAudioComponent is not stopping

I’m trying to stop a bark if another bark is played while the first bark is playing.

The below blueprint in the Level blueprint works:

The equivalent c++ code in the game mode does not stop the audio component:

void AEmzaraGameModeBase::PlayPlayerBark(USoundWave* barkAudio)
{
	if (!barkAudio) return;	
	
	if (playerBarkAudioComponent && playerBarkAudioComponent->IsPlaying())
	{
		playerBarkAudioComponent->Stop();
	}

	playerBarkAudioComponent = UGameplayStatics::SpawnSound2D(this, barkAudio);		
}

While debugging, the stop function is being called, but the audio is not stopping. It just plays the new bark overtop of the old one.

Any idea what is going on here? The project is using engine version 5.4.4.

For what you’re trying to do I suggest using concurrency settings, you limit how many of the same sound play at a time and decide how to stop old sounds. This setting is available in both Cue and meta sound.

Thanks for the response. I actually tried the concurrency settings like you mentioned before writing the post and it had no effect.

I tried it both as an asset and instantiated raw in c++:

FSoundConcurrencySettings ConcurrencySettings;
ConcurrencySettings.MaxCount = 1;
ConcurrencySettings.ResolutionRule = EMaxConcurrentResolutionRule::StopOldest;

USoundConcurrency* SoundConcurrency = NewObject<USoundConcurrency>();
SoundConcurrency->Concurrency = ConcurrencySettings;
   
playerBarkAudioComponent = UGameplayStatics::SpawnSound2D(this, barkAudio, 1.f, 1.f, 0.f, SoundConcurrency, true, false);

The main question is why Stop doesn’t work. :frowning:

I’m gonna delete the binaries and giv’er a rebuild…

Figured it out finally. It was stopping.

There was another piece of code that was playing the same audio file at the same time. One of them was stopped, but the other kept playing.

Ouch, that’s humbling.

1 Like