Hello guys.I don’t really know if I should put it here on in Audio group but it’s code so I’ve chosen to post it here.
I’m struggling with getting PlaybackTime as I mentioned in title.My AudioComponent is created with use of SpawnSound2D engine node. My function now looks like this:
The error i get is : Assertion failed: IsInAudioThread()
Sadly, I’ve never before worked with Sound so I can’t quite understand why it doesn’t work. It crashes exactly with this line:
FActiveSound* ActiveSound = AudioDevice->FindActiveSound(AudioComponent->GetAudioComponentID());
If anyone can point for me why is that I will be so glad :3
You can’t get the Active Sound because you’re trying to run that function / dereference on the Game Thread, but Audio runs in it’s own Audio Thread. If you look at the function definition for FindActiveSound(), it’s failing on the check:
FActiveSound* FAudioDevice::FindActiveSound(const uint64 AudioComponentID)
{
check(IsInAudioThread());
// find the active sound corresponding to this audio component
return AudioComponentIDToActiveSoundMap.FindRef(AudioComponentID);
}
If you want to get the Playback Time of an active sound, the best way is to make a binding to the Playback Delegates in the AudioComponent:
/** called when we finish playing audio, either because it played to completion or because a Stop() call turned it off early */
UPROPERTY(BlueprintAssignable)
FOnAudioPlaybackPercent OnAudioPlaybackPercent;
/** shadow delegate for non UObject subscribers */
FOnAudioPlaybackPercentNative OnAudioPlaybackPercentNative;
Okay I’ve got one more question. It seems that I cannot bind it like I did before with OnAudioFinished. I mean, I’m providing the same arguments but it doesn’t take it
It looks like this:
AudioComponent->OnAudioPlaybackPercent.AddDynamic(this,&UAudioController::SoundFinished);
Does it mean that I can’t use AddDynamic? It says:
no instance of function template matches the argument list
Ok, I’m retarded. I didn’t noticed that this delegate need parameters of const USoundWave* and float. Still i have no clue how cna i get my audio component inside of this function
So I can’t quite make it work What I’ve done for now is this:
1.) I’m Creating AudioComponent.
2.)Then I’m binding delegate like this: AudioComponent->OnAudioPlaybackPercent.AddDynamic(this, &UAudioController::GimmePlaybackPosition);
Function I binded looks like this:
PlaybackTime is static float and AudioComponentID is static uint64. Can you show me some example how to do this properly? No clue here at all, couse this “solution” provides exactly same error