Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - change videos src after clicking on div

Tags:

html

angular

I have problem with video src. After clicking on my div (camera) in source code I can see that video src is changing properly, but videos arent being played. What should I do?

Below are my components - streaming.component.ts (responsible for displaying videos of chosen camera component) and camera.component.ts (for viewing icon with camera name)

streaming.component.ts

import {Component} from '@angular/core';
import {CameraComponent} from '../camera/camera.component';

@Component({
   moduleId: module.id,
   selector: 'streaming',
   template: `
            <camera cameraName="Kamera 1" (click)='cameraStream("sample.mp4")'></camera>
            <camera cameraName="Kamera 2" (click)='cameraStream("sample2.mp4")'></camera>
            <camera cameraName="Kamera 3" (click)='cameraStream("sample3.mp4")'></camera>
            <camera cameraName="Kamera 4" (click)='cameraStream("sample4.mp4")'></camera>
            <video width="800" controls>
                <source src = "{{cameraSrc}}" type="video/mp4">
                Your browser does not support HTML5 video.
            </video>
            <p>{{cameraSrc}}</p>
            `,
directives: [CameraComponent]
})
export class StreamingComponent {

 cameraSrc:string;
 constructor() { }

 cameraStream(chosenCamera :string){
    this.cameraSrc=chosenCamera;
  }
 }

Videos are working ,because when I put

<source src = "{{cameraSrc}}" type="video/mp4">

everything is fine.

camera.component.ts

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

@Component({
    moduleId: module.id,
    selector: 'camera',
    templateUrl: 'camera.component.html'
})
export class CameraComponent {
     @Input() cameraName:string = "";

     constructor() { }

 }

Here is what I get after clicking on camera2 for example .

like image 717
Mateusz Avatar asked Aug 27 '16 11:08

Mateusz


People also ask

How to create radio buttons in angular?

Inside the angular HTML template, define the radio buttons, add the ngModel directive pass the value to it, similarly define the checked state and values. Two divs were initially hidden and get into the visible state on radio button click. Update the code in src/app/app.component.html file.

Where can I find a good tutorial for angular 2?

You should really take a look at the Tour of Heroes, an official intro-tutorial for Angular 2 on their official website. Also, "don't know why but it works" is not really an answer, let alone should be the accepted answer. So explain me, why that way: <source [src]="cameraSrc" type="video/mp4"> it is not working?

How to add click outside directive in CSS?

CSS To add Click Outside directive we will install the ng-click-outside package by running following NPM command in terminal: To use this directive globally in application components, open app.module.ts file then import ClickOutsideModule and also add in imports array as shown below:

Which version of angular is this post compatible with?

This Angular post is compatible with Angular 4 upto latest versions, Angular 7, Angular 8, Angular 9, Angular 10, Angular 11, Angular 12 and Angular 13 We used to have a target element in JQuery using which we could have easily detected if the clicked element is the same element or its outside that element.


2 Answers

Don't know why, but that way:

<video width="800" [src] = "cameraSrc" controls>
    Your browser does not support HTML5 video.
</video>

It is working.

like image 78
Mateusz Avatar answered Oct 11 '22 07:10

Mateusz


Changing src is not enough for <video>, you need to reload it:

constructor(private elRef: ElementRef) { }   // To find elements inside component

cameraStream(chosenCamera: string) {
  this.cameraSrc = chosenCamera;
  const player = this.elRef.nativeElement.querySelector('video');
  player.load();
}

Or more generally, you can make your component watch the changes through the OnChanges contract:

cameraStream(chosenCamera: string) {
  this.cameraSrc = chosenCamera;
}

ngOnChanges(changes: SimpleChanges): void {
  if (changes.cameraSrc) {
    const player = this.elRef.nativeElement.querySelector('video');
    player.load();
  }
}

(Of course you don't need to lookup the player element every time if you can secure its unchanging value before, like in ngOnInit)

like image 16
Javarome Avatar answered Oct 11 '22 07:10

Javarome