Having trouble using a blob as the source for a video file.
Works well on Chrome and Safari, but facing issues on Edge.
This code is written in TypeScript with Angular 7. On Edge mobile, it seems like a broken link is displayed.
private initVideoFromBlob(blob: Blob) {
const edBlob = new Blob([blob], { type: 'video/mp4' });
const url = URL.createObjectURL(edBlob);
this.videoSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
Tried another approach, but encountered the same problem:
private initVideoFromBlob(blob: Blob) {
let url: any;
const edBlob = new Blob([blob], { type: 'video/mp4' });
if (window.navigator.msSaveOrOpenBlob) {
url = window.navigator.msSaveOrOpenBlob(edBlob);
} else {
url = (URL || webkitURL).createObjectURL(edBlob);
}
this.videoSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
My HTML:
<video controls>
<source *ngIf="videoSrc" [src]="videoSrc" type="video/mp4" />
</video>
Edit:
Attempted converting blob to dataURL following DarticCode's suggestion, but still facing the same issue.
private async initVideoFromBlob(blob: Blob) {
let url: string | boolean;
const edBlob = new Blob([blob], { type: 'video/mp4' });
if (/Edge\//.test(navigator.userAgent) || /EdgA\//.test(navigator.userAgent)) {
const promise = new Promise((resolve, reject) => {
const fr = new FileReader();
fr.onload = x => resolve(fr.result);
fr.readAsDataURL(edBlob);
});
url = await promise;
} else {
url = (URL || webkitURL).createObjectURL(edBlob);
}
this.videoSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url);
}