File

src/app/pages/dashboard/rooms/player/player.component.ts

Implements

OnDestroy

Metadata

selector ngx-player
styleUrls player.component.scss
templateUrl ./player.component.html

Index

Properties
Methods
Inputs

Constructor

constructor(playerService: PlayerService)
Parameters :
Name Type Optional
playerService PlayerService no

Inputs

collapsed

Type: boolean

Methods

Private createPlayer
createPlayer()
Returns : void
getProgress
getProgress()
Returns : number
getVolume
getVolume()
Returns : number
next
next()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
playPause
playPause()
Returns : void
prev
prev()
Returns : void
Private reload
reload()
Returns : void
setProgress
setProgress(duration: number)
Parameters :
Name Type Optional
duration number no
Returns : void
Private setTrack
setTrack()
Returns : void
setVolume
setVolume(volume: number)
Parameters :
Name Type Optional
volume number no
Returns : void
toggleLoop
toggleLoop()
Returns : void
toggleShuffle
toggleShuffle()
Returns : void

Properties

player
player: HTMLAudioElement
Type : HTMLAudioElement
shuffle
shuffle: boolean
Type : boolean
track
track: Track
Type : Track
import { Component, HostBinding, Input, OnDestroy } from '@angular/core';
import { PlayerService, Track } from '../../../../@core/data/player.service';

@Component({
  selector: 'ngx-player',
  styleUrls: ['./player.component.scss'],
  templateUrl: './player.component.html',
})
export class PlayerComponent implements OnDestroy {
  @Input()
  @HostBinding('class.collapsed')
  collapsed: boolean;

  track: Track;
  player: HTMLAudioElement;
  shuffle: boolean;

  constructor(private playerService: PlayerService) {
    this.track = this.playerService.random();
    this.createPlayer();
  }

  ngOnDestroy() {
    this.player.pause();
    this.player.src = '';
    this.player.load();
  }

  prev() {
    if (!this.player.loop) {
      if (this.shuffle) {
        this.track = this.playerService.random();
      } else {
        this.track = this.playerService.prev();
      }
    }

    this.reload();
  }

  next() {
    if (!this.player.loop) {
      if (this.shuffle) {
        this.track = this.playerService.random();
      } else {
        this.track = this.playerService.next();
      }
    }

    this.reload();
  }

  playPause() {
    if (this.player.paused) {
      this.player.play();
    } else {
      this.player.pause();
    }
  }

  toggleShuffle() {
    this.shuffle = !this.shuffle;
  }

  toggleLoop() {
    this.player.loop = !this.player.loop;
  }

  setVolume(volume: number) {
    this.player.volume = volume / 100;
  }

  getVolume(): number {
    return this.player.volume * 100;
  }

  setProgress(duration: number) {
    this.player.currentTime = this.player.duration * duration / 100;
  }

  getProgress(): number {
    return this.player.currentTime / this.player.duration * 100 || 0;
  }

  private createPlayer() {
    this.player = new Audio();
    this.player.onended = () => this.next();
    this.setTrack();
  }

  private reload() {
    this.setTrack();
    this.player.play();
  }

  private setTrack() {
    this.player.src = this.track.url;
    this.player.load();
  }
}
<div class="header">My Playlist</div>

<div class="body">

  <div class="track-info">
    <div class="cover" style.background-image="url('{{track.cover}}')"></div>
    <div class="details">
      <h4>{{ track.name }}</h4>
      <span>{{ track.artist }}</span>
    </div>
  </div>

  <div class="progress-wrap">
    <input dir="ltr" type="range" class="progress" [value]="getProgress()" min="0" max="100" step="0.01"
           (input)="setProgress(duration.value)" #duration>
    <div class="progress-foreground" [style.width.%]="getProgress()"></div>
  </div>

  <div class="timing">
    <small class="current">{{ player.currentTime | timing }}</small>
    <small class="remaining">- {{ player.duration - player.currentTime | timing }}</small>
  </div>

  <div class="controls">
    <i class="nb-shuffle shuffle" [class.active]="shuffle" (click)="toggleShuffle()"></i>
    <i class="nb-skip-backward prev" (click)="prev()"></i>
    <i class="play" [class.nb-play]="player.paused" [class.nb-pause]="!player.paused" (click)="playPause()"></i>
    <i class="nb-skip-forward next" (click)="next()"></i>
    <i class="nb-loop loop" [class.active]="player.loop" (click)="toggleLoop()"></i>
  </div>

</div>

<div class="footer">

  <div class="volume">
    <i class="nb-volume-mute"></i>
    <div class="progress-wrap">
      <input type="range" class="progress" [value]="getVolume()" max="100"
             (input)="setVolume(volume.value)" #volume>
      <div class="progress-foreground" [style.width.%]="getVolume()"></div>
    </div>
    <i class="nb-volume-high"></i>
  </div>

</div>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""