UE5 C++ 下载视频到本地。//BindLambda用法解析

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "HttpModule.h"
#include "Interfaces/IHttpRequest.h"
#include "Interfaces/IHttpResponse.h"
#include "GameFramework/Actor.h"
#include "A_Hbt_AsyncDownloadFile.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FDownloadResultDelegate, bool, bSuccess, FString, FilePath);
UCLASS()
class AIEASEMAN3PROJECT_API AA_Hbt_AsyncDownloadFile : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AA_Hbt_AsyncDownloadFile();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	
	/*视频下载*/

public:
	UFUNCTION(BlueprintCallable, Category = "Video Downloader")
	void DownloadVideoInBlueprint(const FString& URL, const FString& SavePath);

private:
	// 下载视频文件


	// HTTP 请求完成时的回调函数
	void OnDownloadComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful, const FString& SavePath);
};
cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.


#include "A_Hbt_AsyncDownloadFile.h"

// Sets default values
AA_Hbt_AsyncDownloadFile::AA_Hbt_AsyncDownloadFile()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AA_Hbt_AsyncDownloadFile::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AA_Hbt_AsyncDownloadFile::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void AA_Hbt_AsyncDownloadFile::DownloadVideoInBlueprint(const FString& URL, const FString& SavePath)
{    // 创建 HTTP 请求
    TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();
    HttpRequest->SetVerb("GET"); // 使用 GET 方法下载文件
    HttpRequest->SetURL(URL);    // 设置下载的 URL

    // 绑定回调函数
    //HttpRequest->OnProcessRequestComplete().BindUObject(this, &AA_Hbt_AsyncDownloadFile::OnDownloadComplete, *SavePath);
    HttpRequest->OnProcessRequestComplete().BindLambda(
        [this, SavePath](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) {
             this->OnDownloadComplete(Request, Response, bWasSuccessful, SavePath);
         }
     );
    // 发送请求
    HttpRequest->ProcessRequest();
}


void AA_Hbt_AsyncDownloadFile::OnDownloadComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful, const FString& SavePath)
{
    if (bWasSuccessful && Response.IsValid())
    {
        // 获取下载的数据
        const TArray<uint8>& Data = Response->GetContent();

        // 将数据写入文件
        if (FFileHelper::SaveArrayToFile(Data, *SavePath))
        {
            UE_LOG(LogTemp, Display, TEXT("视频下载成功,保存路径:%s"), *SavePath);
        }
        else
        {
            UE_LOG(LogTemp, Error, TEXT("文件保存失败!"));
        }
    }
    else
    {
        UE_LOG(LogTemp, Error, TEXT("下载失败!"));
    }
}

用法解析

cpp 复制代码
    HttpRequest->OnProcessRequestComplete().BindLambda(
        [this, SavePath](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) {
             this->OnDownloadComplete(Request, Response, bWasSuccessful, SavePath);
         }
     );

1.Lambda 表达式的基本结构

捕获列表\](参数列表) { 函数体 } 2.用噶 * **`[this, SavePath]`** :捕获列表,表示 Lambda 可以访问当前对象的 `this` 指针和局部变量 `SavePath`。 * **`(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)`** :参数列表,与 `OnProcessRequestComplete` 回调的签名一致。 * **`{ 函数体 }`** :调用成员函数 `OnDownloadComplete`,并传递参数。

相关推荐
AA陈超2 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-06 能力输入的回调
c++·游戏·ue5·游戏引擎·虚幻
AA陈超4 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-29 属性信息委托
c++·游戏·ue5·游戏引擎·虚幻
AA陈超4 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-31 映射标签到属性
c++·游戏·ue5·游戏引擎·虚幻
gshh__4 天前
SuperMap Hi-Fi 3D SDK for Unreal 使用蓝图接口加载多源数据
ue5·游戏引擎·supermap
zhangzhangkeji5 天前
cesium126,230331,Visualize Per-Feature Metadata - 1:官方教程
ue5
zhangzhangkeji5 天前
cesium126,230316,根据经纬度动态生成物体:主要使用了角色的 tag 属性,地球锚点也是有 tag 属性的
ue5
AA陈超5 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-28 构建属性菜单小部件控制器
c++·游戏·ue5·游戏引擎·虚幻
zhangzhangkeji5 天前
UE5 材质-17:水材质系列一 ,panner 平移节点,
ue5·材质
zhangzhangkeji9 天前
UE5 蓝图-24:主 mainUI界面蓝图,主菜单按钮事件定义,拆分按钮,color按钮,退出按钮
ue5
zhangzhangkeji10 天前
UE5 蓝图-11:本汽车蓝图的事件图表,汽车拆分事件,染色事件(绿蓝黄青)。
ue5·1024程序员节