UE4 C++程序播放視頻文件
UE4用編輯器+藍圖方式播放媒體視頻文件很方便,教程也很多。那麼如何在C++代碼中播放呢?
創造,從借鑑(抄襲)開始,引用博客:https://blog.csdn.net/zilisen/article/details/77645500
1.創建MediaPlayer :
2.創建材質:
右擊這玩意,創建材質
3.修改材質類型:
因爲是用在UMG上的
4.創建UMG :UI_VideoView,拖一個image控件放大到全屏,並且設置image的Brush爲上面的材質
5.添加C++類,繼承Actor ,起名Test_PlayMedia。Test_PlayMedia.h添加幾個變量:
public:
//********************************Media*******************************//
//媒體播放器(編輯器中指定)
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "PlayMedia")
UMediaPlayer* mMediaPlayer = nullptr;
//********************************UMG*******************************//
//播放視頻用的UI(編輯器中指定)
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "PlayMedia")
TSubclassOf<UUserWidget> mMediaUIClass;
private:
//********************************Media*******************************//
//********************************UMG*******************************//
UUserWidget* mMediaUI;//主UMG
頭文件是不能少的:
//UMG
#include "UserWidget.h"
//Media
#include "MediaPlayer.h"
哦,不加模塊的話會很慘,在bulid.cs中添加這些
PublicIncludePaths.AddRange(
new string[] {
"Runtime/MediaAssets/Public",//定位到頭文件位置
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
"Runtime/MediaAssets/Private",
// ... add other private include paths required here ...
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
"UMG",//UMG模塊
"MediaAssets",//媒體資源模塊!!!
// ... add other public dependencies that you statically link with here ...
}
);
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
"RHI",
"RenderCore",
// ... add private dependencies that you statically link with here ...
}
);
BeginPlay()中添加如下代碼:
void BeginPlay()
{
Super::BeginPlay();
//1.創建UMG,並且顯示到屏幕
if (mMediaUI == nullptr && mMediaUIClass != nullptr)
{
mMediaUI = CreateWidget<UUserWidget>(GWorld->GetGameInstance(), mMediaUIClass);
}
mMediaUI->AddToViewport();
//2.打開媒體資源
mMediaPlayer->OpenFile("D:/test.mov");
//播放模式
mMediaPlayer->SetLooping(true);//循環播放
//...
}
目前爲止,有畫面,可是沒聲音。
6.添加聲音:
參考博客:https://blog.csdn.net/qq_39393032/article/details/83042776
新加變量:
UMediaSoundComponent* mMediaSoundCmp = nullptr;
添加頭文件:
#include "MediaSoundComponent.h"
構造函數中創建聲音組件:
mMediaSoundCmp = CreateDefaultSubobject<UMediaSoundComponent>(TEXT("MediaSoundCmp"));
BeginPlay()中設置聲音源:
mMediaSoundCmp->SetMediaPlayer(mMediaPlayer);
7.編輯器中指定 mMediaPlayer 和 mMediaUIClass
打開,一切都正常了,能播放視頻,有聲音。
完整代碼參考:
.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
//UMG
#include "UserWidget.h"
//Media
#include "MediaPlayer.h"
#include "MediaSoundComponent.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Test_PlayMedia.generated.h"
UCLASS()
class SENSEVIDEOPROJV1_API ATest_PlayMedia : public AActor
{
GENERATED_BODY()
public:
ATest_PlayMedia();
virtual void Tick(float DeltaTime) override;
protected:
virtual void BeginPlay() override;
private:
public:
//********************************Media*******************************//
//媒體播放器(編輯器中指定)
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "PlayMedia")
UMediaPlayer* mMediaPlayer = nullptr;
//********************************UMG*******************************//
//播放視頻用的UI類(編輯器中指定)
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "PlayMedia")
TSubclassOf<UUserWidget> mMediaUIClass;
private:
//********************************Media*******************************//
UMediaSoundComponent* mMediaSoundCmp = nullptr;
//********************************UMG*******************************//
//顯示視頻UMG
UUserWidget* mMediaUI;
};
.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Test_PlayMedia.h"
ATest_PlayMedia::ATest_PlayMedia()
{
PrimaryActorTick.bCanEverTick = true;
mMediaSoundCmp = CreateDefaultSubobject<UMediaSoundComponent>(TEXT("MediaSoundCmp"));
}
void ATest_PlayMedia::BeginPlay()
{
Super::BeginPlay();
//1.創建UMG,並且顯示到屏幕
if (mMediaUI == nullptr && mMediaUIClass != nullptr)
{
mMediaUI = CreateWidget<UUserWidget>(GWorld->GetGameInstance(), mMediaUIClass);
}
mMediaUI->AddToViewport();
//2.打開媒體資源(自己換成相對路徑)
mMediaPlayer->OpenFile("D:/test.mov");
//播放模式
mMediaPlayer->SetLooping(true);//循環播放
//...
//設置聲音的媒體源
mMediaSoundCmp->SetMediaPlayer(mMediaPlayer);
}
void ATest_PlayMedia::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
能拖藍圖解決的問題爲什麼要作跟自己過不去呢?
因爲代碼是程序的尊嚴。