Skip to content
Snippets Groups Projects
media-player-control-bar.component.ts 5.67 KiB
Newer Older
  • Learn to ignore specific revisions
  •   OnInit, OnChanges, SimpleChanges, OnDestroy, Component, EventEmitter, Input, Output
    
    } from '@angular/core';
    import { MatSliderChange } from '@angular/material/slider';
    
    import { PlayerProperties, ValueChangeElement } from '../../interfaces/elements';
    
      selector: 'aspect-media-player-control-bar',
      templateUrl: './media-player-control-bar.component.html',
      styleUrls: ['./media-player-control-bar.component.css']
    
    export class MediaPlayerControlBarComponent implements OnInit, OnChanges, OnDestroy {
    
      @Input() player!: HTMLVideoElement | HTMLAudioElement;
    
      @Input() id!: string;
    
      @Input() savedPlaybackTime!: number;
    
      @Input() playerProperties!: PlayerProperties;
    
      @Input() project!: 'player' | 'editor';
    
      @Input() active!: boolean;
    
      @Input() dependencyDissolved!: boolean;
    
      @Output() elementValueChanged = new EventEmitter<ValueChangeElement>();
    
      @Output() onMediaValidStatusChanged = new EventEmitter<string>();
    
    
      duration!: number;
      currentTime!: number;
    
      currentRestTime!: number;
      started!: boolean;
    
      playing!: boolean;
      pausing!: boolean;
      runCounter!: number;
      lastVolume!: number;
    
      restTimeMode: boolean = true;
      showHint!: boolean;
      disabled!: boolean;
    
      ngOnInit(): void {
    
        this.playbackTime = this.savedPlaybackTime || this.playerProperties.playbackTime;
    
        this.started = this.playbackTime > 0;
        this.runCounter = Math.floor(this.playbackTime);
    
        this.player.ondurationchange = () => this.initTimeValues();
    
        this.player.ontimeupdate = () => {
          this.currentTime = this.player.currentTime / 60;
          this.currentRestTime = this.player.duration ? (this.player.duration - this.player.currentTime) / 60 : 0;
    
          this.sendPlaybackTimeChanged();
    
        };
        this.player.onpause = () => {
          this.playing = false;
          this.pausing = true;
        };
        this.player.onplaying = () => {
          this.playing = true;
          this.pausing = false;
          this.started = true;
          this.showHint = false;
        };
        this.player.onended = () => {
    
          this.checkValidState(this.runCounter + 1);
    
          if (!this.checkDisabledState(this.runCounter + 1)) {
    
            this.runCounter += 1;
    
            if (this.playerProperties.loop) {
    
              this._play();
            }
          }
        };
        this.player.onvolumechange = () => {
          this.player.muted = !this.player.volume;
        };
    
        this.player.volume = this.playerProperties.defaultVolume;
    
        this.lastVolume = this.player.volume;
    
      ngOnChanges(changes: SimpleChanges): void {
    
        if (this.project !== 'editor' && changes.dependencyDissolved && changes.dependencyDissolved.currentValue) {
    
        this.player.pause();
      }
    
      onTimeChange(event: MatSliderChange): void {
        this.player.currentTime = event.value ? event.value : 0;
      }
    
      onVolumeChange(event: MatSliderChange): void {
    
        event.source.value = event.value && event.value > this.playerProperties.minVolume ?
          event.value : this.playerProperties.minVolume;
    
        this.player.volume = event.source.value;
    
      toggleTime(): void {
    
        this.restTimeMode = !this.restTimeMode;
      }
    
    
      toggleVolume(): void {
    
        if (this.player.volume > this.playerProperties.minVolume) {
    
          this.lastVolume = this.player.volume;
    
          this.player.volume = this.playerProperties.minVolume;
    
        } else {
          this.player.volume = this.lastVolume;
        }
      }
    
    
      private checkValidState(runCounter: number): boolean {
    
        this.valid = this.playerProperties.minRuns === 0 ? true : runCounter >= this.playerProperties.minRuns;
    
          this.onMediaValidStatusChanged.emit(this.id);
    
        }
        return this.valid;
      }
    
      private checkDisabledState(runCounter: number): boolean {
    
        this.disabled = !this.playerProperties.maxRuns ? false : this.playerProperties.maxRuns <= runCounter;
    
        this.initAutostart();
        if (!this.started) {
          this.initHint();
    
      private initAutostart(): void {
    
        if (this.playerProperties.autostart) {
    
            if (this.dependencyDissolved && !this.disabled) {
    
          }, this.playerProperties.autostartDelay);
    
        if (this.playerProperties.hintLabel) {
    
            if (!this.started && this.dependencyDissolved) {
    
          }, this.playerProperties.hintLabelDelay);
    
        this.player.play().then(() => {},
          // eslint-disable-next-line no-console
          () => console.error('player: cannot play this media file'));
    
        if (this.player.currentTime > 0 || this.runCounter > 0) {
          this.elementValueChanged.emit({
            id: this.id,
            value: this.toPlaybackTime()
          });
        }
    
        this.playbackTime = this.player.duration ?
          this.runCounter + this.player.currentTime / this.player.duration : this.playbackTime;
        return this.playbackTime;
    
        if (!this.duration) {
          if ((this.player.duration !== Infinity) && this.player.duration) {
            this.duration = this.player.duration / 60;
    
            this.player.currentTime = (this.playbackTime - this.runCounter) * this.player.duration;
    
            this.currentRestTime = (this.player.duration - this.player.currentTime) / 60;
    
            this.checkDisabledState(this.runCounter);
            this.checkValidState(this.runCounter);
    
            this.sendPlaybackTimeChanged();
    
          } else {
            this.duration = 0;
    
    
      ngOnDestroy(): void {
        this.player.pause();
      }