Angular 2 - change videos src after clicking on di

2020-04-03 13:23发布

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 .

标签: html angular
7条回答
时光不老,我们不散
2楼-- · 2020-04-03 13:29

Don't know why, but that way:

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

It is working.

查看更多
家丑人穷心不美
3楼-- · 2020-04-03 13:31

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)

查看更多
▲ chillily
4楼-- · 2020-04-03 13:32

I had a similar problem... I settled with using a backing array

<span *ngFor="let video of app.videoPlayer.videos">
<img src="{{video.artBig()}}" style="width: 1920px;height: 1080px;top: 0px;left:0px;z-index: -1; position: absolute;"/>

<video preload="auto" crossorigin controls autoplay
   style="width: 1920px;height: 1080px;top: 0px;left:0px;z-index:1; position: absolute;">
  <source src="{{video.url()}}" type="{{video.mimeType()}}">
</video>
</span>

Whenever my array is emptied the video component disappears... if my array changes the component gets renew.

Works like a charm, hope it helps.

查看更多
相关推荐>>
5楼-- · 2020-04-03 13:41

In html template use video tag

<video #video id="video" width="100%" height="100%" controls></video>

<mat-icon (click)="playVideo(url)">
            play_arrow
          </mat-icon>

In Component

@ViewChild('video')

public video: ElementRef;

playVideo(url: string) {
    this.video.nativeElement.src = url;
    this.video.nativeElement.load();
    this.video.nativeElement.play();
  }
查看更多
该账号已被封号
6楼-- · 2020-04-03 13:47

Seems like the src attribute of the <source> element doesn't like to be dynamic. Try changing your markup to be:

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

That way the DOM element only gets created when there is indeed a value in cameraSrc.

查看更多
闹够了就滚
7楼-- · 2020-04-03 13:48

in Angular 6, Here is the sample.

    <div style="text-align:center">
  <div *ngFor="let item of data" >
    <div class="module hero">
      <a href="javascript: void(0)" (click)="showVideo(item)">
       {{item.name}}
        </a>
    </div>
  </div>
</div>
<div *ngIf="selectedItem">
  <video controls autoplay #myVideo>
    <source *ngIf="selectedItem" [src]="selectedItem.url" type="video/mp4" />
    Browser not supported
</video>
</div>

And Component code is

showVideo(item: any){
    this.selectedItem =null;
    this.selectedItem =item;  
    this.myVideo.nativeElement.src = item.url; 
  }
查看更多
登录 后发表回答