求助,如何用C++写播放视频功能,在slate控件上播放?

[复制链接]
查看2657 | 回复6 | 2017-4-7 17:51:23 | 显示全部楼层 |阅读模式
有例子不,参考一下
kiben4 | 2017-4-8 08:45:01 | 显示全部楼层
刚好我也打算同问呢
回复 支持 反对

使用道具 举报

StaticMao | 2017-4-10 10:51:40 | 显示全部楼层
mknmknmk 发表于 2017-4-7 19:41
搜void FStreamingPauseRenderingModule::BeginStreamingPause( FViewport* GameViewport )

感谢大神提供线索
回复 支持 反对

使用道具 举报

StaticMao | 2017-4-10 10:52:37 | 显示全部楼层
kiben4 发表于 2017-4-8 08:45
刚好我也打算同问呢

mknmknmk提供的方法你试了吗,我今天刚看到
回复 支持 反对

使用道具 举报

StaticMao | 2017-4-17 17:53:00 | 显示全部楼层
已经实现,现在把代码贴上来,给大家参考
[mw_shl_code=cpp,true]// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "UObject/WeakObjectPtr.h"
#include "Widgets/DeclarativeSyntaxSupport.h"
#include "Widgets/SCompoundWidget.h"

class UAudioComponent;
class UMaterial;
class UMaterialExpressionTextureSample;
class UMediaPlayer;
class UMediaSoundWave;
class UMediaTexture;
struct FSlateBrush;
enum class EMediaEvent;
/**
*
*/

class USERINTERFACE_API SVideoPlay : public SCompoundWidget
{
public:
        DECLARE_DELEGATE(FCloseVieoPlay)
        SLATE_BEGIN_ARGS(SVideoPlay)
        {}
        SLATE_EVENT(FCloseVieoPlay,OnCloseVideoPlay)
        SLATE_ARGUMENT(FString, VideoURL)
                SLATE_END_ARGS()


        SVideoPlay();
        ~SVideoPlay();
        /** Constructs this widget with InArgs */
        void Construct(const FArguments& InArgs);

        /** The material that wraps the video texture for display in an SImage. */
        UMaterial* Material;

        /** The Slate brush that renders the material. */
        TSharedPtr<FSlateBrush> MaterialBrush;

        /** The media player whose video texture is shown in this widget. */
        UMediaPlayer* MediaPlayer;

        UMediaTexture* DefaultTexture;

        TWeakObjectPtr<UMediaTexture> CurrentTexture;

        /** The video texture sampler in the wrapper material. */
        UMaterialExpressionTextureSample* TextureSampler;

        virtual void Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime) override;

        void UpdateMaterial();

        FString VideoURL;

        FReply CloseVideoPlay();

        FCloseVieoPlay OnCloseVideoPlay;
};
[mw_shl_code=cpp,true]// Fill out your copyright notice in the Description page of Project Settings.

#include "UserInterface.h"
#include "SVideoPlay.h"
#include "Styling/SlateBrush.h"
#include "Widgets/Images/SImage.h"
#include "IMediaOutput.h"
#include "IMediaPlayer.h"
#include "Materials/Material.h"
#include "Materials/MaterialExpressionTextureSample.h"
#include "MediaPlayer.h"
#include "MediaTexture.h"
#include "Engine.h"
#include "SlateOptMacros.h"


SVideoPlay::SVideoPlay() :
        CurrentTexture(nullptr)
        , DefaultTexture(nullptr)
        , Material(nullptr)
        , MediaPlayer(nullptr)
{
        //DefaultSoundWave = NewObject<UMediaSoundWave>(GetTransientPackage(), NAME_None, RF_Transient | RF_Public);

        //if (DefaultSoundWave != nullptr)
        //{
        //        DefaultSoundWave->AddToRoot();

        //        AudioComponent = FAudioDevice::CreateComponent(DefaultSoundWave);

        //        if (AudioComponent != nullptr)
        //        {
        //                AudioComponent->SetVolumeMultiplier(1.0f);
        //                AudioComponent->SetPitchMultiplier(1.0f);
        //                AudioComponent->bAllowSpatialization = false;
        //                AudioComponent->bIsUISound = true;
        //                AudioComponent->bAutoDestroy = false;
        //                AudioComponent->AddToRoot();
        //        }
        //}
}
SVideoPlay::~SVideoPlay()
{
        if (MediaPlayer != nullptr)
        {
                MediaPlayer->OnMediaEvent().RemoveAll(this);

                // remove default sinks from native player
                //FMediaPlayerBase& Player = MediaPlayer->GetBasePlayer();

                /*if (MediaPlayer->GetSoundWave() == nullptr)
                {
                        Player.SetAudioSink(nullptr);
                }*/

                /*if (MediaPlayer->GetVideoTexture() == nullptr)
                {
                        Player.SetVideoSink(nullptr);
                }*/
        }

        // release default sinks
        /*if (AudioComponent != nullptr)
        {
                AudioComponent->Stop();
                AudioComponent->RemoveFromRoot();
        }*/

        if (Material != nullptr)
        {
                Material->RemoveFromRoot();
        }

        /*if (DefaultSoundWave != nullptr)
        {
                DefaultSoundWave->RemoveFromRoot();
        }*/

        if (DefaultTexture != nullptr)
        {
                DefaultTexture->RemoveFromRoot();
        }
}
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SVideoPlay::Construct(const FArguments& InArgs)
{
        OnCloseVideoPlay = InArgs._OnCloseVideoPlay;
        VideoURL = InArgs._VideoURL;
        MediaPlayer = LoadObject<UMediaPlayer>(NULL, TEXT("/Game/Common/Video/MediaPlayer.MediaPlayer"), NULL, LOAD_None, NULL);

        Material = NewObject<UMaterial>(GetTransientPackage(), NAME_None, RF_Transient | RF_Public);

        if (Material != nullptr)
        {
                TextureSampler = NewObject<UMaterialExpressionTextureSample>(Material);
                {
                        TextureSampler->SamplerType = SAMPLERTYPE_Color;
                }

                FExpressionOutput& Output = TextureSampler->GetOutputs()[0];
                FExpressionInput& Input = Material->EmissiveColor;
                {
                        Input.Expression = TextureSampler;
                        Input.Mask = Output.Mask;
                        Input.MaskR = Output.MaskR;
                        Input.MaskG = Output.MaskG;
                        Input.MaskB = Output.MaskB;
                        Input.MaskA = Output.MaskA;
                }

                Material->Expressions.Add(TextureSampler);
                Material->MaterialDomain = EMaterialDomain::MD_UI;
                Material->AddToRoot();
        }

        //// create Slate brush
        MaterialBrush = MakeShareable(new FSlateBrush());
        {
                MaterialBrush->SetResourceObject(Material);
        }

        UpdateMaterial();
        ChildSlot
                [
                        SNew(SBox)
                        .WidthOverride(1000)
                        .HeightOverride(1000)
                        .HAlign(HAlign_Center)
                        .VAlign(VAlign_Center)
                        [
                                SNew(SVerticalBox)
                                +SVerticalBox::Slot()
                                .AutoHeight()
                                [
                                        SNew(SButton)
                                        .ButtonStyle(FPanelStyle::Get(), "Button_alpha")
                                        .OnClicked(this, &SVideoPlay::CloseVideoPlay)
                                        .HAlign(HAlign_Right)
                                        .VAlign(VAlign_Center)
                                        .Content()
                                        [
                                                SNew(SImage)
                                                .Image(FPanelStyle::GetBrush("scheme_close"))
                                        ]
                                ]
                                +SVerticalBox::Slot()
                                [
                                        SNew(SImage)
                                        .Image(MaterialBrush.Get())
                                ]
                       
                        ]
                ];


        //bool IsPlay = MediaPlayer->OpenUrl(TEXT("http://127.0.0.1:8080/Marionette_RL.mp4"));
        bool IsPlay = MediaPlayer->OpenUrl(VideoURL);
       
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION

void SVideoPlay::Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime)
{
        UpdateMaterial();
        //UpdateSoundWave();
}

void SVideoPlay::UpdateMaterial()
{
        UMediaTexture* Texture = MediaPlayer->GetVideoTexture();

        //if (Texture == nullptr)
        //{
        //        // create default texture if needed
        //        if (DefaultTexture == nullptr)
        //        {
        //                DefaultTexture = NewObject<UMediaTexture>(GetTransientPackage(), NAME_None, RF_Transient | RF_Public);

        //                if (DefaultTexture != nullptr)
        //                {
        //                        DefaultTexture->UpdateResource();
        //                        DefaultTexture->AddToRoot();
        //                }
        //        }

        //        // set default texture as output sink
        //        if (DefaultTexture != nullptr)
        //        {
        //                MediaPlayer->GetBasePlayer().SetVideoSink(DefaultTexture);
        //        }

        //        Texture = DefaultTexture;
        //}

        // assign new texture to material
        if (Texture != CurrentTexture)
        {
                TextureSampler->Texture = Texture;
                Material->PostEditChange();
                CurrentTexture = Texture;
        }

        // resize current texture
        if (CurrentTexture != nullptr)
        {
               
                MaterialBrush->ImageSize.X = CurrentTexture->GetSurfaceWidth();
                MaterialBrush->ImageSize.Y = CurrentTexture->GetSurfaceHeight();
        }
        else
        {
                MaterialBrush->ImageSize = FVector2D::ZeroVector;
        }
}
FReply SVideoPlay::CloseVideoPlay()
{
        if (OnCloseVideoPlay.IsBound())
        {
                OnCloseVideoPlay.Execute();
        }
        //OnCloseVideoPlay.ExecuteIfBound();
        return FReply::Handled();
}[/mw_shl_code][/mw_shl_code]
回复 支持 反对

使用道具 举报

StaticMao | 2017-4-17 17:53:56 | 显示全部楼层
额,论坛的代码排版这个差
回复 支持 反对

使用道具 举报

07xfyan | 2017-4-20 14:47:09 | 显示全部楼层
谢谢分享。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

13

主题

73

回帖

26

积分

初阶编码师

积分
26