How can I play a video using C++?

Hello.

I have the following video ->

I’ve been able to make it play in my level using a widget and with the following level BP (for the sound I just added a plane and added a sound to it as component):

However, I’d like to do it all (play the video and its sound) using just C++. Could someone give me an example please?

Thanks

Open up your code solution, search for “CreateWidget” (or use ALT+SHIFT+S if you have Visual Assist installed), you should see it pop up UserWidget.h. Same with MediaPlayer/OpenSource/etc.

In the end you end up with something like




#include "UMG/Blueprint/UserWidget.h"
#include "MediaAssets/MediaPlayer.h"

UIntroWidget* NewIntroWidget = CreateWidget<UIntroWidget>(GetWorld(), UIntroWidget::StaticClass()); // Class name will be different if you made it from a Blueprint, something like UIntroWidget_C or some such.

UMediaPlayer* MyMediaPlayer = NewObject<UMediaPlayer>();
MyMediaPlayer->Play();
MyMediaPlayer->OpenSource(IntroMovie); // Assuming you have UMediaSource* named IntroMovie like in your BP.



etc.

Blueprints are thin wrappers around C++ code. So if you have a blueprint, you can find out exactly what C++ it is using just by searching through the code a bit. If you aren’t building the UE4 Engine from the Git source, I highly suggest you do it - even if you don’t plan to modify it as being able to read the code and walk through things in a debugger is invaluable.

1 Like