I’m trying to load videos dynamically from memory in a project, and this is causing a particular headache for WebGL. On standalone and mobile, I could just save this file temporarily on disk and load it from there, but that’s not possible on WebGL.
To try and work around this, I’m sending the video data to JS and creating a blob, then using URL.createObjectURL. However, after I set the url property of a VideoPlayer, then call Play(), the url property seems to get reset to an empty string, and the video doesn’t play.
.jslib code:
mergeInto(LibraryManager.library, {
CreateBlob: function(array, size)
{
var blobData = HEAPU8.subarray(array, array + size);
var blob = new Blob([blobData], { type: "video/mp4" })
var url = URL.createObjectURL(blob);
var bufferSize = lengthBytesUTF8(url) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(url, buffer, bufferSize);
return buffer;
}
});
C# code
public VideoPlayer vid;
[DllImport("__Internal")]
private static extern string CreateBlob(byte[] array, int size);
[...]
void PlayVideo(byte[] videoData)
{
string url = CreateBlob(videoData, videoData.Length);
Debug.Log("Video URL before: " + url);
vid.url = url;
vid.Play();
Debug.Log("Video URL after: " + vid.url);
}
The above C# logs out:
I know the issue isn’t with the video or the process of converting it into a blob: URL, as I can open URL returned from CreateBlob in another tab, and it plays the video just fine.
I’ve seen other threads around (for example, here ) that seem to be a result of Unity incorrectly parsing blob: URL’s. Is this what’s happening here, or is there a fundamental problem with playing video’s from blob: URL’s?
Thanks,
Stephen