如何在Angular 6的htmlvideoelement元素中更改currenttime

时间:2018-10-27 07:46:27

标签: java spring typescript current-time

我不反对如何设置htmlvideoelement的当前时间。我想通过单击进度栏更改视频的当前时间。请帮助我正确地称呼。可能知道交钥匙解决方案。

html 

<video controls
  width="300"
  height="200"
  (click)="onClick()"
>
</video>

typescript

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


@Component({
  selector: 'app-player',
  templateUrl: './player.component.html',
  styleUrls: ['./player.component.css']
})
export class PlayerComponent implements AfterContentInit {
  value = 100;

  constructor() { }
  videoElement: HTMLVideoElement;
  ngAfterContentInit() {
    this.videoElement = <HTMLVideoElement>document.getElementsByTagName('video')[0];
    this.videoElement.src = 'http://localhost:8090/video/Pre_Int_02_90815.mp4';
    this.videoElement.volume = this.value;
    this.videoElement.currentTime = 10.0;
  }
}

有关Java代码的控制器:

@RequestMapping(method = RequestMethod.GET, value = "/{video:.+}")
public StreamingResponseBody stream(@PathVariable String video)
			throws FileNotFoundException {
	File videoFile = videos.get(video);
	final InputStream videoFileStream = new FileInputStream(videoFile);
	return (os) -> {
		readAndWrite(videoFileStream, os);
   		};
}

1 个答案:

答案 0 :(得分:0)

1)使用getElementByTagName JavaScript

setCurrentTime(seconds) {
    const video = document.getElementsByTagName('video')[0];
    try {
            video.currentTime = seconds;
        } catch (e) {
              console.log(e);
        }
}

2)在Angular 6+中使用ElementRef

// your HTML should be
// <video #target class="video-js" width="100%" height="100%" controls>
//      <source [src]="yourVideoUrl">
// </video>

import videojs from 'video.js';
export class Yourcomponent implements AfterViewInit {
@ViewChild('target') target: ElementRef;

player: videojs.Player;
 ngAfterViewInit() {
    // instantiate Video.js
    if (this.videoURL) {
      this.player = videojs(this.target.nativeElement, {
        autoplay: true,
        controlls: true
      }, () => {
        console.log('onPlayerReady', this);
      });
    }

  }
setCurrentTime(seconds) {
        try {
              this.target.nativeElement.currentTime = seconds;
        } catch (e) {
              console.log(e);
        }
    }
}

3)您必须通过HTML页面进行事件 像

<button (click)="setCurrentTime(4)"> // pass the time in seconds 
  change Time
</button>