导入音频
在运行时导入音频的过程可以分为几个步骤:
- 创建运行时音频导入器。
- 绑定到所需的委托(OnProgress 和 OnResult)。
- 从文件或缓冲区导入音频。
- 播放从 OnResult 委托获得的导入音频波形(更多信息在这里)。
确保 Runtime Audio Importer 和 Sound Wave 实例不会因垃圾回收而过早销毁,可以通过使用 UPROPERTY()、TStrongObjectPtr 或其他方法将它们分配给单独的变量来保持对它们的强引用。
Blueprint示例
首先,你需要创建一个 Runtime Audio Importer 对象。你应该确保它由垃圾回收器作为强引用处 理,这可以通过在Blueprint中将其作为单独的变量进行设置来实现。这可以防止对象的过早销毁。
- Blueprint
- C++
// UPROPERTY() is used here to prevent the object from being prematurely garbage collected
UPROPERTY()
class URuntimeAudioImporterLibrary* Importer;
Importer = URuntimeAudioImporterLibrary::CreateRuntimeAudioImporter();
要跟踪导入音频数据的进度,可以绑定到 OnProgress
(Blueprint)/ OnProgressNative
(C++)委托。这样可以监控进度,并例如实现加载屏幕。要绑定此委托,只需从绑定事件节点拉出事件引脚即可。
- Blueprint
- C++
// Assuming Importer is a UE reference to a URuntimeAudioImporterLibrary object
// AddWeakLambda is used just as an example. You can use any other method to bind the delegate, such as AddUObject, AddUFunction, etc.
Importer->OnProgressNative.AddWeakLambda(this, [](int32 Percentage)
{
UE_LOG(LogTemp, Log, TEXT("Import progress: %d"), Percentage);
});
要在音频数据导入过程完成时收到通知并访问生成的声波的引用,您必须绑定到 OnResult
(Blueprints) / OnResultNative
(C++) 委托。同时,确保导入的声波被垃圾收集器视作强引用,以防止不必要的提前垃圾收集。这可以通过在 Blueprints 中将其作为单独的变量进行设置。
- Blueprint
- C++
// Assuming Importer is a UE reference to a URuntimeAudioImporterLibrary object
// AddWeakLambda is used just as an example. You can use any other method to bind the delegate, such as AddUObject, AddUFunction, etc.
Importer->OnResultNative.AddWeakLambda(this, [](URuntimeAudioImporterLibrary* Importer, UImportedSoundWave* ImportedSoundWave, ERuntimeImportStatus Status)
{
UE_LOG(LogTemp, Log, TEXT("Import result: %s"), *UEnum::GetValueAsString(Status));
});
通过调用相关函数开始音频导入过程,该函数可以处理压缩和未压缩的音频数据格式。
- Blueprint
- C++
// Assuming Importer is a UE reference to a URuntimeAudioImporterLibrary object
// Import audio from a file
Importer->ImportAudioFromFile(TEXT("C:/Folder/AudioFile.mp3"), ERuntimeAudioFormat::Auto);
// Import audio from a buffer
TArray<uint8> AudioData = ...; // Fill the array with your audio data
Importer->ImportAudioFromBuffer(MoveTemp(AudioData), ERuntimeAudioFormat::OggVorbis);
// Import audio from a RAW file
Importer->ImportAudioFromRAWFile(TEXT("C:/Folder/AudioFile.raw"), ERuntimeRAWAudioFormat::Int8, 44100, 2);
// Import audio from a RAW buffer
TArray<uint8> RAWBuffer = ...; // Fill the array with your PCM int 16-bit audio data
Importer->ImportAudioFromRAWBuffer(MoveTemp(RAWBuffer), ERuntimeRAWAudioFormat::Int16, 44100, 2);
一旦导入过程完成,导入的声音波形可以通过 OnResult
委托获得。
完整示例
- Blueprint
- C++
这是一个从文件导入音频的基本代码示例。
示例使用了位于 EXAMPLEMODULE
模块中 UImportAudioClassExample
类的 ImportAudioExample
函数。
注意:为了成功运行示例,请确保在 .Build.cs 文件的 PublicDependencyModuleNames
或 PrivateDependencyModuleNames
中添加 RuntimeAudioImporter
模块,并且在项目的 .uproject 文件中也添加该模块。
#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);
}