Was loading wav file using a different code but switched to using ogg format (it’s already compressed and takes less time to play than wav) as wav file was getting decompressed on playing it the first time which caused the freezing for long depending on the length of the audio. Using ogg also causes a small freeze but its 1-2 seconds at max compared to wav taking 8-10 seconds. I want to avoid this small hiccup when playing. Here’s the code for loading ogg at runtime:
USoundWave* sw = NewObject<USoundWave>(USoundWave::StaticClass());
if (!sw)
{
UE_LOG(LogTemp, Warning, TEXT("Sound Wave Object Not Valid"));
}
sw->SoundGroup = ESoundGroup::SOUNDGROUP_Default;
TArray < uint8 > rawFile;
//FilePath is a string variable that holds the path to the ogg file including filename and extension
bool loaded = FFileHelper::LoadFileToArray(rawFile, FilePath.GetCharArray().GetData());
if (loaded)
{
FByteBulkData* bulkData = &sw->CompressedFormatData.GetFormat(TEXT("OGG"));
bulkData->Lock(LOCK_READ_WRITE);
FMemory::Memcpy(bulkData->Realloc(rawFile.Num()), rawFile.GetData(), rawFile.Num());
bulkData->Unlock();
sw->NumChannels = 2;
//Setting duration manually for the time being, need to figure out a way to do it dynamically
sw->Duration = 225.0f;
UE_LOG(LogTemp, Warning, TEXT("OGG File Loaded Successfully"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("OGG File Could Not Be Loaded"));
}