I am trying to display a random video on a plane object.
So far I am only able to display a specific video on a plane object at any given time:
I could really use the help.
I am trying to display a random video on a plane object.
So far I am only able to display a specific video on a plane object at any given time:
I could really use the help.
You should be able to pick a random video to play if you add a MediaSource variable to your Blueprint and change it to an Array. Then on BeginPlay you can use a Random Integer in Range node to randomly select the index of a video from that array, then pass that video into the MediaPlayer. Here’s an example of the node setup:
Inside your array of sources, you just need to add each media source you want to have a chance of playing and one will be picked at random when the game starts.
Edit: be sure to check that there is at least one video in the list before calling Open Source. If you overlook this (as I did) and pass in an empty array, you will probably crash.
I hope this helps!
Thanks my good guy, it works perfectly! One more question I have is if I could make it so after one video is done player, it will randomly select another video to play.
You can use the MediaPlayer’s On End Reached event for this. When you select the MediaPlayer variable in your Blueprint, you can click the big green plus next to the On End Reached event at the bottom of the Details panel.
Once you click this, it will add that event to the Event Graph. I’d recommend moving the nodes from my main post into a function rather than copying it over. Then you can hook that function into the event and it’ll look a little something like this:
With this current setup, it will randomly select any video in the list, so it can replay previous ones, including the one that just finished. If you wanted to avoid this, you could add code to the random video function that removes the selected index so it won’t be played again. Also, I added a check to make sure the list isn’t empty because currently you run the risk of trying to get a non-existent video, which isn’t a good idea. Here’s the updated function:
Even if you don’t add the code to remove played videos, you should add the check to make sure there’s at least one video in the list before you try playing one. If you don’t, you could end up with an index out of bounds error, resulting in a crash. Sorry for overlooking that in my original post.
Thanks so much, you really helped me out.