Import audio
The process of importing audio at runtime can be broken down into several steps:
- Create a Runtime Audio Importer.
- Bind to the needed delegates (OnProgress and OnResult).
- Import audio from a file or buffer.
- Play the imported sound wave obtained from the OnResult delegate (more info is here).
Ensure that both Runtime Audio Importer and Sound Wave instances are not prematurely garbage collected by maintaining a hard reference to them, which can be done by assigning them to separate variables using UPROPERTY(), TStrongObjectPtr, or any other method that prevents the object from being destroyed.
Blueprints example
First, you need to create a Runtime Audio Importer object. Also ensure it is treated as a strong reference by the garbage collector, which can be achieved by placing it as a separate variable in Blueprints. This prevents premature destruction of the object.
To track the progress of importing audio data, you can bind to the OnProgress
delegate. This will allow you to monitor the progress and, for example, implement a loading screen. To bind to this delegate, you can simply pull the event pin from the bind event node.
To be notified when the audio data import process is complete and to access the reference of the resulting sound wave, you must bind to the OnResult
delegate. Also, make sure the imported sound wave is treated as a strong reference by the garbage collector to prevent unwanted premature garbage collection. This can be done by placing it as a separate variable in Blueprints.
Begin the audio import process by calling the relevant function, which can handle both compressed and uncompressed audio data formats.
Once the import process is complete, the resulting imported sound wave can be obtained through the OnResult
delegate.
C++ example
This is a basic code example for importing audio from a file.
The example uses the ImportAudioExample
function located in the UImportAudioClassExample
class within the EXAMPLEMODULE
module.
Note: To successfully run the example, make sure to add the RuntimeAudioImporter
module to either PublicDependencyModuleNames
or PrivateDependencyModuleNames
in the .Build.cs file, as well as to your project's .uproject file.
#pragma once
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "ImportAudioClassExample.generated.h"
UCLASS(BlueprintType)
class EXAMPLEMODULE_API UImportAudioClassExample : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
void ImportAudioExample();
private:
// Please pay attention to making the RuntimeAudioImporter a hard reference, such as using UPROPERTY(), to prevent it from being prematurely garbage collected
UPROPERTY()
class URuntimeAudioImporterLibrary* RuntimeAudioImporter;
};
#include "ImportAudioClassExample.h"
#include "RuntimeAudioImporterLibrary.h"
void UImportAudioClassExample::ImportAudioExample()
{
RuntimeAudioImporter = URuntimeAudioImporterLibrary::CreateRuntimeAudioImporter();
if (!IsValid(RuntimeAudioImporter))
{
UE_LOG(LogTemp, Error, TEXT("Failed to create audio importer"));
return;
}
RuntimeAudioImporter->OnProgressNative.AddWeakLambda(this, [](int32 Percentage)
{
UE_LOG(LogTemp, Log, TEXT("Audio importing percentage: %d"), Percentage);
});
RuntimeAudioImporter->OnResultNative.AddWeakLambda(this, [this](URuntimeAudioImporterLibrary* Importer, UImportedSoundWave* ImportedSoundWave, ERuntimeImportStatus Status)
{
if (Status == ERuntimeImportStatus::SuccessfulImport)
{
UE_LOG(LogTemp, Warning, TEXT("Successfully imported audio with sound wave %s"), *ImportedSoundWave->GetName());
// Here you can handle ImportedSoundWave playback, like "UGameplayStatics::PlaySound2D(GetWorld(), ImportedSoundWave);"
}
else
{
UE_LOG(LogTemp, Error, TEXT("Failed to import audio"));
}
RuntimeAudioImporter = nullptr;
});
RuntimeAudioImporter->ImportAudioFromFile(TEXT("C:/Folder/Example.mp3"), ERuntimeAudioFormat::Auto);
}