Pixel Streaming 音频捕获
Pixel Streaming 是一种用于 Unreal Engine 的插件,通过 WebRTC 流式传输渲染帧并同步输入/输出。应用程序在服务器端运行,而客户端负责渲染和用户交互。有关 Pixel Streaming 和设置的更多详细信息,请参阅 Pixel Streaming 文档。
兼容性
此解决方案适用于:
- 官方 Pixel Streaming 基础设施(Epic Games 参考实现)
- 第三方 Pixel Streaming 提供商,包括:
- Vagon.io
- Arcane Mirage
- Eagle 3D Streaming
- 其他基于 WebRTC 的流媒体解决方案
- 操作系统:Windows 和 Linux 服务器
已在这些环境中进行测试并能够正常运行,不受 Pixel Streaming 托管解决方案的影响。
扩展插件安装
此功能作为 Runtime Audio Importer 插件的扩展提供。要使用此功能,您需要:
- 确保项目中已安装 Runtime Audio Importer 插件
- 从 Google Drive 下载 Pixel Streaming Audio Capture 插件扩展
- 从下载的压缩包中解压文件夹到项目的
Plugins
文件夹中(如果不存在该文件夹,请创建) - 重建您的项目(此扩展需要一个 C++ 项目)
- 该扩展以源代码形式提供,使用时需要一个 C++ 项目
- 支持的 Unreal Engine 版本:UE 5.2 及更高版本
- 有关如何手动构建插件的更多信息,请参阅 构建插件教程
概述
Pixel Streaming Capturable Sound Wave 扩展了标准的 Capturable Sound Wave,允许直接从 Pixel Streaming 客户端的麦克风捕获音频。此功能使您能够:
- 捕获通过 Pixel Streaming 连接的浏览器的音频
- 处理来自特定玩家/同行的音频
- 实现语音聊天、语音命令或远程用户的音频录制
基本用法
创建 Pixel Streaming Capturable Sound Wave
首先,您需要创建一个 Pixel Streaming Capturable Sound Wave 对象:
- Blueprint
- C++
PixelStreamingSoundWave = UPixelStreamingCapturableSoundWave::CreatePixelStreamingCapturableSoundWave();
您应该将Pixel Streaming Capturable Sound Wave视为一个强引用,以防止其被过早销毁(例如,通过在Blueprints中将其分配给一个单独的变量或在C++中使用UPROPERTY()
)。
启动和停止捕获
您可以通过简单的函数调用来启动和停止音频捕获:
- Blueprint
- C++
// Assuming PixelStreamingSoundWave is a reference to a UPixelStreamingCapturableSoundWave object
// Start capturing audio (the device ID parameter is ignored for Pixel Streaming)
PixelStreamingSoundWave->StartCapture(0);
// Stop capturing audio
PixelStreamingSoundWave->StopCapture();
StartCapture
中的 DeviceId
参数对于 Pixel Streaming Capturable Sound Waves 是忽略的,因为捕获源是由系统自动确定的或通过您设置的播放器信息决定的。
检查捕获状态
您可以检查 sound wave 是否正在捕获音频:
- Blueprint
- C++
// Assuming PixelStreamingSoundWave is a reference to a UPixelStreamingCapturableSoundWave object
bool bIsCapturing = PixelStreamingSoundWave->IsCapturing();
完整示例
以下是一个关于如何设置 Pixel Streaming 音频捕获的完整示例:
- Blueprint
- C++
这是一个从 Pixel Streaming 客户端捕获音频数据的基本代码示例。
该示例使用位于 EXAMPLEMODULE
模块内的 UPixelStreamingAudioExample
类中的 CapturePixelStreamingAudioExample
函数。
为了成功运行示例,请确保在 .Build.cs 文件中的 PublicDependencyModuleNames
或 PrivateDependencyModuleNames
中添加 RuntimeAudioImporter
和 PixelStreaming
模块,并在项目的 .uproject 文件中添加这些模块。
#pragma once
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "PixelStreamingAudioExample.generated.h"
UCLASS(BlueprintType)
class EXAMPLEMODULE_API UPixelStreamingAudioExample : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
void CapturePixelStreamingAudioExample();
private:
// Keep a strong reference to prevent garbage collection
UPROPERTY()
class UPixelStreamingCapturableSoundWave* PixelStreamingSoundWave;
};
#include "PixelStreamingAudioExample.h"
#include "Sound/PixelStreamingCapturableSoundWave.h"
#include "Kismet/GameplayStatics.h"
void UPixelStreamingAudioExample::CapturePixelStreamingAudioExample()
{
// Create a Pixel Streaming Capturable Sound Wave
PixelStreamingSoundWave = UPixelStreamingCapturableSoundWave::CreatePixelStreamingCapturableSoundWave();
// Start capturing audio
PixelStreamingSoundWave->StartCapture(0);
// Play the sound wave to hear the captured audio
UGameplayStatics::PlaySound2D(GetWorld(), PixelStreamingSoundWave);
// Set up a timer to stop capture after 30 seconds (just for demonstration)
FTimerHandle TimerHandle;
GetWorld()->GetTimerManager().SetTimer(TimerHandle, [this]()
{
PixelStreamingSoundWave->StopCapture();
}, 30.0f, false);
}
使用多个 Pixel Streaming 播放器
在有多个 Pixel Streaming 客户端同时连接的场景中,您可能需要从特定播放器捕捉音频。以下功能可以帮助您进行管理。
获取可用的 Pixel Streaming 播放器
要识别哪些 Pixel Streaming 播放器已连接:
- Blueprint
- C++
UPixelStreamingCapturableSoundWave::GetAvailablePixelStreamingPlayers(FOnGetAvailablePixelStreamingPlayersResultNative::CreateWeakLambda(this, [](const TArray<FPixelStreamingPlayerInfo_RAI>& AvailablePlayers)
{
// Handle the list of available players
for (const FPixelStreamingPlayerInfo_RAI& PlayerInfo : AvailablePlayers)
{
UE_LOG(LogTemp, Log, TEXT("Available player: %s on streamer: %s"), *PlayerInfo.PlayerId, *PlayerInfo.StreamerId);
}
}));
设置玩家以进行捕获
当您需要从特定玩家捕获时:
- Blueprint
- C++
// Assuming PixelStreamingSoundWave is a reference to a UPixelStreamingCapturableSoundWave object
// and PlayerInfo is a FPixelStreamingPlayerInfo_RAI object from GetAvailablePixelStreamingPlayers
PixelStreamingSoundWave->PlayerInfo = PlayerInfo;
如果您将 Player ID 留空,声波将自动监听第一个连接的可用玩家。这是默认行为,并适用于单玩家场景。
常见用例
语音聊天实现
您可以使用 Pixel Streaming Capturable Sound Waves 在远程用户和本地玩家之间实现语音聊天:
- 为每个连接的玩家创建一个 Pixel Streaming Capturable Sound Wave
- 设置一个系统来管理当前正在讲话的玩家
- 使用 Voice Activity Detection 系统检测用户何时讲话
- 如有需要,使用 Unreal Engine 的音频系统对音频进行空间化处理
使用语音识别进行语音命令
您可以通过将此功能与 Runtime Speech Recognizer 插件结合,实现 远程用户的语音命令识别:
- 使用 Pixel Streaming Capturable Sound Wave 从 Pixel Streaming 客户端捕获音频
- 将捕获的音频直接传递给 Runtime Speech Recognizer
- 在您的游戏逻辑中处理识别的文本
只需在 Runtime Speech Recognizer 示例中,将标准的 Capturable Sound Wave 替换为 Pixel Streaming Capturable Sound Wave,它将无缝地与 Pixel Streaming 音频输入配合使用。
录制远程用户音频
您可以录制远程用户的音频以便日后回放:
- 使用 Pixel Streaming Capturable Sound Wave 捕获音频
- 使用 Export Audio 将捕获的音频导出到文件
- 保存文件供以后使用或分析