# Correct way to create mediaplayer in 4.19

**URL:** <https://forums.unrealengine.com/t/correct-way-to-create-mediaplayer-in-4-19/421287>\
**Category:** Cinematics & Media\
**Tags:** UE4-19, access-violation, unreal-engine, CPP, question\
**Created:** [March 30, 2018, 1:28am UTC](https://forums.unrealengine.com/t/correct-way-to-create-mediaplayer-in-4-19/421287 "2018-03-30T01:28:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mctittles](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mctittles](https://forums.unrealengine.com/u/Mctittles)\
**Post date:** [March 30, 2018, 1:28am UTC](https://forums.unrealengine.com/t/correct-way-to-create-mediaplayer-in-4-19/421287/1 "2018-03-30T01:28:09Z")

</div>

I’m converting my project from 4.18. I have UMediaPlayer\* MediaPlayer; in header and this in BegingPlay():

```
MediaPlayer = NewObject<UMediaPlayer>();
			NewMediaSoundComponent = NewObject<UMediaSoundComponent>(this);
			NewMediaSoundComponent->SetMediaPlayer(MediaPlayer);
			//NewMediaSoundComponent->MediaPlayer = MediaPlayer;
			NewMediaSoundComponent->bAllowSpatialization = 0;
			NewMediaSoundComponent->bIsUISound = true;
			NewMediaSoundComponent->SetActive(true);
			NewMediaSoundComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
			NewMediaSoundComponent->UpdatePlayer();

			MediaPlayer->PlayOnOpen = false;
			MediaPlayer->OnMediaOpened.Add(mediaOpenedDelegate);
			MediaPlayer->OnMediaOpenFailed.Add(mediaOpenedDelegate);
			MediaPlayer->OnMediaClosed.Add(mediaClosedDel);
			MediaPlayer->OnEndReached.Add(endReachedDel);
			MediaPlayer->OnPlaybackResumed.Add(playbackResumedDel);

```

One thing I noticed right away was you need to SetMediaPlayer instead of assigning the variable. I left the old code commented out and is only thing I changed. However I’m getting an access violation error when trying to do anything on the MediaPlayer object now. For instance if I do this:

```
MediaPlayer->Close();

```

It will crash with access violation error.

---

<div class="post-metadata">

**Author:** ![gmpreussner\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gmpreussner\_1](https://forums.unrealengine.com/u/gmpreussner_1)\
**Post date:** [April 2, 2018, 8:30pm UTC](https://forums.unrealengine.com/t/correct-way-to-create-mediaplayer-in-4-19/421287/2 "2018-04-02T20:30:16Z")

</div>

Make sure you assign _NewMediaSoundComponent_ and _MediaPlayer_ to some other Actor’s or _UObject_’s _UPROPERTY_, or call _AddToRoot()_ to attach them to the root set, otherwise they will get garbage collected shortly after you created them.

Take a look at _SMediaPlayerEditorOutput::Construct_, for example.

When you’re done using your objects, make sure you clear the property that holds them / remove them from the root set, otherwise you’ll leak memory.
