Binding to MediaPlayer events in C++

Noa Winged
Mon, 08 Feb 2021 09:55:30 +0000
Community & Industry Discussion

How to get notified when MediaPlayer broadcasts an event?
To be specific I am trying to get notified about media that has been opened.
_
I found in UMediaPlayer code that I should bind my UFUNCTION to:

/** Multicast delegate that is invoked when a media player's media has been opened. */
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnMediaPlayerMediaOpened, FString, OpenedUrl);
FOnMediaPlayerMediaOpened OnMediaOpened;

_

but I can’t make my binding work:

// Header file
class MYAPI_API AMyActor : public AActor
{
	...
private:
	UMediaPlayer* MyMediaPlayer;
	UImgMediaSource* MySource;
	UMediaTexture* MyMediaTexture;

	UFUNCTION()
	void CatchMediaOpened(FString OpenedUrl);
	...
}

_

// CPP file
AMyActor::AMyActor()
{
	...
	MyMediaPlayer = NewObject<UMediaPlayer>(GetTransientPackage(), MakeUniqueObjectName(GetTransientPackage(), UMediaPlayer::StaticClass()));
	MyMediaPlayer->AddToRoot(); // Without this line adding delegate to Media Player event crashes the editor

	FScriptDelegate CatchMediaOpenedDelegate;
	CatchMediaOpenedDelegate.BindUFunction(this, FName(TEXT("CatchMediaOpened")));
	MyMediaPlayer->OnMediaOpened.AddUnique(CatchMediaOpenedDelegate);
	...
}

void AMyActor::LoadMediaSource()
{
	...
	MyMediaTexture->SetMediaPlayer(MyMediaPlayer);
	MyMediaPlayer->OpenSource(MySource);
	MyMediaPlayer->Play();
	...
}

void AMyActor::CatchMediaOpened(FString OpenedUrl)
{
	UE_LOG(LogTemp, Warning, TEXT("Debug: %s"), *OpenedUrl);
}

_
Could sbd point out what am I doing wrong? I tried to create MediaPlayer by my own inside editor too, but it also didn’t work. Binding dynamic multicast event in blueprint actor works correctly and I would like to do the same in code.

4 posts - 1 participant