# Binding to MediaPlayer events in C++

**URL:** <https://forums.unrealengine.com/t/binding-to-mediaplayer-events-in-c/478432>\
**Category:** Community & Industry Discussion\
**Tags:** ufunction, binding, unreal-engine, question\
**Created:** [February 8, 2021, 9:55am UTC](https://forums.unrealengine.com/t/binding-to-mediaplayer-events-in-c/478432 "2021-02-08T09:55:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoaWinged](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/noawinged/32/1099439_2.png) [@NoaWinged](https://forums.unrealengine.com/u/NoaWinged)\
**Post date:** [February 8, 2021, 9:55am UTC](https://forums.unrealengine.com/t/binding-to-mediaplayer-events-in-c/478432/1 "2021-02-08T09:55:30Z")

</div>

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.

 ![329139-ue4editor-ib0fsiivxs.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/5/2/3523b4e554e7a83746b26300f7d240f863cd7b25.jpeg)

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [February 8, 2021, 3:56pm UTC](https://forums.unrealengine.com/t/binding-to-mediaplayer-events-in-c/478432/2 "2021-02-08T15:56:15Z")

</div>

Hello! Can you replace this

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

```

with this

```
 MyMediaPlayer->OnMediaOpened.AddDynamic(this, &AMyActor::CatchMediaOpened);

```

And one more thing - declare you prop like this

UPROPERTY()  
UMediaPlayer\* MyMediaPlayer;

and you can remove adding to root logic.

---

<div class="post-metadata">

**Author:** ![NoaWinged](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/noawinged/32/1099439_2.png) [@NoaWinged](https://forums.unrealengine.com/u/NoaWinged)\
**Post date:** [February 8, 2021, 5:51pm UTC](https://forums.unrealengine.com/t/binding-to-mediaplayer-events-in-c/478432/3 "2021-02-08T17:51:37Z")

</div>

There is no exposed `AddDynamic` function in MediaPlayer class… Or am I wrong?

 ![329202-devenv-fervbmy423.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/1/f/b1f29d9bdfaa4d649cbd451e01b09e1cc396e88b.png)

@UPROPERTY() - Should I declare variables with this specifier if I want to create and manage objects only inside C++ and don’t expose it to blueprints?

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [February 8, 2021, 6:05pm UTC](https://forums.unrealengine.com/t/binding-to-mediaplayer-events-in-c/478432/4 "2021-02-08T18:05:41Z")

</div>

AddDynamic is a MACRO and in fact it is in the list, but it is named there as \_\_internal\_AddDynamic. As for UPROPERTY - this macro guards you pointer from Garbage collection, also it is used to expose in Blueprints and at last is used in serialization and reflection.
