I'm trying to stream a video in angular but for some reason, it's not loading I have added the code below
HTML
<div>
<video controls #videoElement ></video>
</div>
Ts file
mimeCodec = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';// i have make sure the file is mp4 type only
ngAfterViewInit() {
if (
"MediaSource" in window &&
MediaSource.isTypeSupported(this.mimeCodec)
) {
const mediaSource = new MediaSource();
(this.video.nativeElement as HTMLVideoElement).src = URL.createObjectURL(
mediaSource
);
mediaSource.addEventListener("sourceopen", () =>
this.sourceOpen(mediaSource)
);
} else {
console.error("Unsupported MIME type or codec: ", this.mimeCodec);
}
}
sourceOpen(mediaSource) {
const sourceBuffer = mediaSource.addSourceBuffer(this.mimeCodec);
return this.http
.get(this.assetURL, { responseType: "blob" })
.subscribe((blob) => {
sourceBuffer.addEventListener("updateend", () => {
mediaSource.endOfStream();
this.video.nativeElement.play();
});
blob.arrayBuffer().then((x) => sourceBuffer.appendBuffer(x));
});
}
server-side -Asp.net core
[HttpGet("ActivityVideo/{id:int}")]
[AllowAnonymous]
public IActionResult GetVideo(int id)
{
try
{
var video = _videoService.GetVideo(id);
return PhysicalFile(video.Path, "application/octet-stream", enableRangeProcessing: true);
}
catch (Exception ex)
{
}
return null;
}
when debug the call is hitting the server. and getting the response same from the postman as well. in postman I can save the response as mp4 then the video is playing. so I think something is wrong with my angular code. can anyone please suggest the issue or if you have any other way to stream a video that will be fine too?
Note: I'm using Jwt to authenticate so I cant put the URL in src just for testing I'm allowing the video to be anonymous Thanks.