可捕获声波
概述
可捕获声波派生自流式声波。它支持从麦克风等输入设备捕获音频数据并进行回放,具有与导入声波相同的功能(包括倒放、在SoundCue中使用等)。您甚至可以同时捕获和播放音频,没有任何限制。
关于语音活动检测(VAD),请参阅此页面。
在iOS和Android平台上,可捕获声波会在开始捕获时自动请求麦克风权限。
基础用法
创建可捕获声波
首先需要创建一个可捕获声波。
- 蓝图
- C++
UCapturableSoundWave* CapturableSoundWave = UCapturableSoundWave::CreateCapturableSoundWave();
您应将可捕获的声波视为强引用以防止提前销毁(例如通过将其赋值给蓝图中的独立变量或使用C++中的UPROPERTY()
)。
获取可用输入设备
要使用此功能,您需要先获取所有可用于捕获的音频输入设备。
- Blueprint
- C++
UCapturableSoundWave::GetAvailableAudioInputDevices(FOnGetAvailableAudioInputDevicesResultNative::CreateWeakLambda(this, [](const TArray<FRuntimeAudioInputDeviceInfo>& AvailableDevices)
{
// Handle the result
}));
开始与停止捕获
获取可用设备后,您可以开始从输入设备捕获音频数据,并在需要时停止捕获。
- Blueprint
- C++
// Assuming CapturableSoundWave is a UE reference to a UCapturableSoundWave object (or its derived type)
// Start capturing audio data from the input device with the specified device ID (0 in this case)
CapturableSoundWave->StartCapture(0);
// Stop capturing audio data
CapturableSoundWave->StopCapture();
DeviceId 是设备在可用设备数组中的索引,该数组通过前一步骤获取。
检查捕获状态
您可以检查当前是否正在捕获声波音频:
- Blueprint
- C++
// Assuming CapturableSoundWave is a reference to a UCapturableSoundWave object
bool bIsCapturing = CapturableSoundWave->IsCapturing();
高级功能
静音与取消静音采集
您可以选择静音或取消静音采集,在不中断采集过程的前提下防止音频数据堆积。
- Blueprint
- C++
// Assuming CapturableSoundWave is a UE reference to a UCapturableSoundWave object (or its derived type)
CapturableSoundWave->ToggleMute(true);
当需要频繁切换捕获时,此方法非常有用,因为通过StartCapture
开始录制可能会在引擎端造成轻微卡顿。您应当先启动捕获本身,然后再进行静音/取消静音操作。
若目标是消除轻微冻结现象,可考虑在卡顿影响可忽略的时机启动捕获。例如:在游戏加载画面期间启动捕获,随后立即将可捕获声波静音,待需要捕获音频数据时再取消静音,即可实现无卡顿的音频采集。
完整示例
以下是使用可捕获声波的完整实现示例:
- 蓝图
- C++
这是使用可捕获声波从输入设备(麦克风)捕获音频数据的基础代码示例。
该示例使用了位于EXAMPLEMODULE
模块中UCaptureAudioClassExample
类内的CaptureAudioExample
函数。
要成功运行示例,请确保将RuntimeAudioImporter
模块添加到**.Build.cs文件的PublicDependencyModuleNames
或PrivateDependencyModuleNames
中,同时添加到项目的.uproject**文件。
#pragma once
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "CaptureAudioClassExample.generated.h"
UCLASS(BlueprintType)
class EXAMPLEMODULE_API UCaptureAudioClassExample : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
void CaptureAudioExample();
private:
// Please pay attention to making the StreamingSoundWave a hard reference, such as using UPROPERTY(), to prevent it from being prematurely garbage collected
UPROPERTY()
class UCapturableSoundWave* CapturableSoundWave;
};
#include "CaptureAudioClassExample.h"
#include "Sound/StreamingSoundWave.h"
void UCaptureAudioClassExample::CaptureAudioExample()
{
// Create a capturable sound wave
CapturableSoundWave = UCapturableSoundWave::CreateCapturableSoundWave();
// Capture audio data from the input device with the specified device ID (0 in this case)
CapturableSoundWave->StartCapture(0);
// Delay for 5 seconds, just for demonstration purposes
FTimerHandle TimerHandle;
GetWorld()->GetTimerManager().SetTimer(TimerHandle, [this]()
{
// Stop capturing audio data
CapturableSoundWave->StopCapture();
// Play the sound wave
UGameplayStatics::PlaySound2D(GetWorld(), CapturableSoundWave);
}, 5, false);
}