Hi :), I’m triying to preload a video before use it in order to improve the video’s fluency. So I unchecked the option PlayOnAwake of the VideoPlayer.
Then I added the next code
But the problem is that, if I uncheck the PlayOnAwake, it seems that the videoElement doesn’t exists. So I can’t preload the video. Do you know some alternative in order to preload it ?
I tried also, leave the PlayOnAwake and pause the videoElement and then resume it but It did’t work as I expected.
Hello there – we have an experimental AI bot that might be able to help you with your question. Would you like to try this out?
How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.
It seems that the VideoPlayer component does indeed hold the reference to the HTMLVideoElement you are trying to manipulate. When playOnAwake is unchecked and you are trying to access the video element, it may not be initialized properly.
Instead of relying on playOnAwake, you should manually initialize the video element using the create method provided by the VideoPlayer component. This method allows you to create the video element without immediately playing it.
Here’s an updated version of your code that initializes the video element and then preloads and plays the video:
export class VideoLoadController extends Behavior {
@serializable(VideoPlayer)
videoPlayer!: VideoPlayer;
start(): void {
// Ensure the VideoPlayer is assigned
if(this.videoPlayer) {
// Manually create the video element without auto-playing
this.videoPlayer.create(false);
// Access the video element
const video = this.videoPlayer.videoElement;
if (video) {
// Set preload attribute and load the video
video.preload = "auto";
video.load();
// Listen for the 'canplaythrough' event to start playback
video.addEventListener("canplaythrough", () => {
video.play();
});
}
}
}
}