How To Record Video In Angular 12

In this article, we will learn how to record video and how to make blob URL in Angular 12.

What is Blob URL?

There are clouds available that provide a service as BLOB (Binary Large Object) storage, which supports the storage of large amounts of unstructured object data, such as text or binary data. eg- Azure Blob storage.


  • Basic knowledge of Angular.
  • Code editor likes VS Code.

First, create a new project in Angular using this command.

ng new AngularVideoDemo

Open the app.component.ts and paste the following code into it.

import { Component, ViewChild, ElementRef } from '@angular/core';

declare var MediaRecorder: any;

  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent {
  @ViewChild('recordedVideo') recordVideoElementRef: ElementRef | any;
  @ViewChild('video') videoElementRef: ElementRef | any;

  htmlVideoElement: HTMLVideoElement | any;
  recordVideoElement: HTMLVideoElement | any;
  mediaRecorder: any;
  recordedBlobs: Blob[] | any;
  isRecording: boolean = false;
  downloadUrl: string | any;
  stream: MediaStream | any;

  constructor() { }

  ngOnInit() {
        video: {
          width: 360
      .then(stream => {
        this.htmlVideoElement = this.videoElementRef.nativeElement;
        this.recordVideoElement = this.recordVideoElementRef.nativeElement; = stream;
        this.htmlVideoElement.srcObject =;

  startRecording() {
    this.recordedBlobs = [];
    let options: any = { mimeType: 'video/webm' };

    this.mediaRecorder = new MediaRecorder(, options);

    this.isRecording = !this.isRecording;

  stopRecording() {
    this.isRecording = !this.isRecording;

  playRecording() {
    if (!this.recordedBlobs || !this.recordedBlobs.length) {
      console.log('cannot play.');

  onDataAvailableEvent() {
    try {
      this.mediaRecorder.ondataavailable = (event: any) => {
        if ( && > 0) {
    } catch (error) {

  onStopRecordingEvent() {
    this.mediaRecorder.onstop = (event: Event) => {
      const videoBuffer = new Blob(this.recordedBlobs, {
        type: 'video/webm'
      this.downloadUrl = window.URL.createObjectURL(videoBuffer);
      this.recordVideoElement.src = this.downloadUrl;

Open the app.component.html and paste the following code into it.

<div style="text-align:center">
  <video class="video" #video autoplay></video>
  <span class="m-1"></span>
  <button class="btn btn-primary btn-lg" *ngIf="!isRecording" (click)="startRecording()">Start Recording</button>
  <button class="btn btn-warning btn-lg" *ngIf="isRecording" (click)="stopRecording()">Stop Recording</button>
  <video class="video" style="width:360 !important;" controls #recordedVideo>

Now add this CDN to your main index.html

<link rel="stylesheet" href="">
<script src=""></script>
<script src=""></script>



In this article, we have learned about recording videos and how to make blob URL in Angular 12. You can download the code from my Github repository.

If you have any questions or issues about this article, please let me know.

Also, check How To Capture Photo Using ngx-webcam In Angular 12

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories