UE4 C++程序播放视频文件

                                    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);

}

能拖蓝图解决的问题为什么要作跟自己过不去呢?

因为代码是程序的尊严。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章