npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@libs-ui/components-audio

v0.2.352-3

Published

## Giới thiệu

Readme

Audio Component

Giới thiệu

audio là một component mạnh mẽ dùng để phát âm thanh trong ứng dụng Angular. Component này cung cấp giao diện đơn giản để phát, tạm dừng và điều khiển các tệp âm thanh với nhiều tính năng.

Tính năng

  • Phát/tạm dừng âm thanh
  • Điều chỉnh âm lượng với chức năng tắt/bật tiếng
  • Hiển thị thời gian theo định dạng HH:MM:SS
  • Thanh tiến độ với chức năng tua nhanh/tua lại
  • Tải xuống audio với kiểm soát quyền
  • Function Control API cho phép điều khiển từ bên ngoài
  • Thiết kế responsive
  • Source audio có thể cấu hình

Cài đặt

Để cài đặt component audio, sử dụng npm hoặc yarn:

npm install @libs-ui/components-audio

hoặc

yarn add @libs-ui/components-audio

Sử dụng

Import module

import { LibsUiComponentsAudioComponent } from '@libs-ui/components-audio';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, LibsUiComponentsAudioComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}

Hoặc trong component standalone:

import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LibsUiComponentsAudioComponent } from '@libs-ui/components-audio';
import { IAudioFunctionControlEvent } from '@libs-ui/components-audio';

@Component({
  selector: 'app-example',
  standalone: true,
  imports: [CommonModule, LibsUiComponentsAudioComponent],
  template: `
    <libs_ui-components-audio
      [fileAudio]="audioSource"
      [checkPermissionDownloadAudio]="checkDownloadPermission"
      (outFunctionsControl)="registerFunctions($event)"></libs_ui-components-audio>

    <div class="controls">
      <button (click)="playAudio()">Phát/Tạm dừng</button>
      <button (click)="toggleMute()">Bật/Tắt tiếng</button>
    </div>
  `,
})
export class ExampleComponent {
  audioSource = 'path/to/audio/file.mp3';
  functionControls: IAudioFunctionControlEvent | null = null;

  checkDownloadPermission(): Promise<boolean> {
    // Kiểm tra quyền download
    return Promise.resolve(true);
  }

  registerFunctions(event: IAudioFunctionControlEvent) {
    this.functionControls = event;
  }

  playAudio() {
    if (this.functionControls) {
      this.functionControls.playPause();
    }
  }

  toggleMute() {
    if (this.functionControls) {
      this.functionControls.toggleMute();
    }
  }
}

API Reference

Inputs

| Tên | Kiểu | Mặc định | Mô tả | | ---------------------------- | ------------------------ | -------- | ---------------------------------------------------------------------------- | | fileAudio | string | required | URL của file audio cần phát. | | checkPermissionDownloadAudio | () => Promise<boolean> | required | Function trả về promise với kết quả boolean cho biết nếu được phép download. |

Outputs

| Tên | Kiểu | Mô tả | | ------------------- | ------------------------------------------- | ------------------------------------------------------- | | outFunctionsControl | IAudioFunctionControlEvent | Event chứa các hàm điều khiển của audio component. | | outVolumeControl | number | Phát ra giá trị âm lượng hiện tại (0-100). | | outTimeUpdate | { currentTime: string, duration: string } | Phát ra thông tin thời gian hiện tại và tổng thời gian. | | outEnded | void | Phát ra khi audio kết thúc phát. | | outMute | boolean | Phát ra trạng thái tắt/bật tiếng. | | outPlay | boolean | Phát ra trạng thái phát/tạm dừng. |

Các phương thức (qua outFunctionsControl)

| Tên phương thức | Tham số | Kiểu trả về | Mô tả | | --------------- | --------------- | ----------- | ------------------------------------------------ | | playPause | event?: Event | void | Bắt đầu/tạm dừng phát audio. | | toggleMute | event?: Event | void | Bật/tắt âm thanh. | | setVolume | value: number | void | Điều chỉnh âm lượng (0-100). | | seekTo | value: number | void | Di chuyển đến vị trí cụ thể trong audio (0-100). | | download | event?: Event | void | Tải xuống file audio. | | isPlaying | - | boolean | Kiểm tra trạng thái đang phát audio. | | isMuted | - | boolean | Kiểm tra trạng thái tắt tiếng. |

Interfaces

// Audio Function Control Event
interface IAudioFunctionControlEvent {
  playPause: (event?: Event) => void;
  toggleMute: (event?: Event) => void;
  setVolume: (value: number) => void;
  seekTo: (value: number) => void;
  download: (event?: Event) => void;
  isPlaying: () => boolean;
  isMuted: () => boolean;
}

Styling Volume Slider

Để tạo hiệu ứng thanh trượt âm lượng có màu nền thay đổi theo giá trị, bạn có thể sử dụng CSS variables:

/* Định nghĩa thanh trượt âm lượng */
input[type='range'].volume-slider {
  background: linear-gradient(to right, #3b82f6 var(--volume-percent, 50%), #e5e7eb var(--volume-percent, 50%));
}

/* Track styling cho WebKit browsers */
input[type='range'].volume-slider::-webkit-slider-runnable-track {
  background: linear-gradient(to right, #3b82f6 var(--volume-percent, 50%), #e5e7eb var(--volume-percent, 50%));
}

/* Track styling cho Firefox */
input[type='range'].volume-slider::-moz-range-track {
  background: linear-gradient(to right, #3b82f6 var(--volume-percent, 50%), #e5e7eb var(--volume-percent, 50%));
}

Và trong template:

<input
  type="range"
  min="0"
  max="100"
  [value]="volumePercent()"
  (input)="changeVolume($event)"
  class="volume-slider"
  [style.--volume-percent.%]="volumePercent()" />

Ví dụ

Sử dụng Function Control

import { Component, signal, computed } from '@angular/core';
import { IAudioFunctionControlEvent } from '@libs-ui/components-audio';

@Component({
  selector: 'app-example',
  template: `
    <libs_ui-components-audio
      [fileAudio]="audioSource()"
      [checkPermissionDownloadAudio]="checkDownloadPermission"
      (outFunctionsControl)="registerFunctions($event)"></libs_ui-components-audio>

    <div class="audio-controls">
      <button (click)="playPauseAudio()">{{ isPlaying() ? 'Tạm dừng' : 'Phát' }}</button>
      <button (click)="toggleMuteAudio()">{{ isMuted() ? 'Bật tiếng' : 'Tắt tiếng' }}</button>
      <div class="volume-control">
        <span>Âm lượng:</span>
        <input
          type="range"
          min="0"
          max="100"
          [value]="volumePercent()"
          (input)="changeVolume($event)"
          class="volume-slider"
          [style.--volume-percent.%]="volumePercent()" />
        <span>{{ volumePercent() }}%</span>
      </div>
      <div class="progress-control">
        <span>Tiến độ:</span>
        <input
          type="range"
          min="0"
          max="100"
          [value]="progress()"
          (input)="changeProgress($event)" />
        <span>{{ progress() }}%</span>
      </div>
      <button (click)="downloadAudio()">Tải xuống</button>
    </div>
  `,
})
export class ExampleComponent {
  audioSource = signal('path/to/audio.mp3');
  functionControls: IAudioFunctionControlEvent | null = null;
  isPlaying = signal(false);
  isMuted = signal(false);
  volume = signal(80);
  progress = signal(0);

  // Computed properties
  volumePercent = computed(() => Math.round(this.volume()));

  checkDownloadPermission = (): Promise<boolean> => {
    return Promise.resolve(true);
  };

  registerFunctions(event: IAudioFunctionControlEvent) {
    this.functionControls = event;

    // Initialize state
    if (this.functionControls) {
      this.isPlaying.set(this.functionControls.isPlaying());
      this.isMuted.set(this.functionControls.isMuted());
    }
  }

  playPauseAudio() {
    if (this.functionControls) {
      this.functionControls.playPause();
      this.isPlaying.set(this.functionControls.isPlaying());
    }
  }

  toggleMuteAudio() {
    if (this.functionControls) {
      this.functionControls.toggleMute();
      this.isMuted.set(this.functionControls.isMuted());
    }
  }

  changeVolume(event: Event) {
    if (this.functionControls && event.target) {
      const value = parseInt((event.target as HTMLInputElement).value);
      this.volume.set(value);
      this.functionControls.setVolume(value / 100); // Convert to 0-1 range
    }
  }

  changeProgress(event: Event) {
    if (this.functionControls && event.target) {
      const value = parseInt((event.target as HTMLInputElement).value);
      this.progress.set(value);
      this.functionControls.seekTo(value);
    }
  }

  downloadAudio() {
    if (this.functionControls) {
      this.functionControls.download();
    }
  }
}

Sử dụng Events

Sử dụng các events để phản ứng với thay đổi từ audio player:

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

@Component({
  selector: 'app-example',
  template: `
    <libs_ui-components-audio
      [fileAudio]="audioSource()"
      [checkPermissionDownloadAudio]="checkPermission"
      (outTimeUpdate)="handleTimeUpdate($event)"
      (outVolumeControl)="handleVolumeChange($event)"
      (outPlay)="handlePlayChange($event)"
      (outMute)="handleMuteChange($event)"
      (outEnded)="handleEnded()"></libs_ui-components-audio>

    <div class="audio-info">
      <p>Trạng thái: {{ isPlaying() ? 'Đang phát' : 'Tạm dừng' }}</p>
      <p>Thời gian hiện tại: {{ currentTime() }}</p>
      <p>Tổng thời gian: {{ duration() }}</p>
      <p>Âm lượng: {{ volumeLevel() }}%</p>
    </div>
  `,
})
export class ExampleComponent {
  audioSource = signal('path/to/audio.mp3');
  isPlaying = signal(false);
  isMuted = signal(false);
  currentTime = signal('00:00:00');
  duration = signal('00:00:00');
  volumeLevel = signal(100);

  checkPermission = (): Promise<boolean> => {
    return Promise.resolve(true);
  };

  handleTimeUpdate(timeInfo: { currentTime: string; duration: string }) {
    this.currentTime.set(timeInfo.currentTime);
    this.duration.set(timeInfo.duration);
  }

  handleVolumeChange(volume: number) {
    this.volumeLevel.set(volume);
  }

  handlePlayChange(isPlaying: boolean) {
    this.isPlaying.set(isPlaying);
  }

  handleMuteChange(isMuted: boolean) {
    this.isMuted.set(isMuted);
  }

  handleEnded() {
    this.isPlaying.set(false);
  }
}