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

yy-audio-player

v1.0.0

Published

音频播放工具,支持播放、暂停、切换等操作

Readme

yy-audio-player

音频播放工具,支持播放、暂停、切换等操作。

安装

npm install yy-audio-player

使用

基础用法

import { createAudioPlayer } from 'yy-audio-player';

// 创建音频播放器实例
const player = createAudioPlayer({
  onPlay: () => {
    console.log('开始播放');
  },
  onPause: () => {
    console.log('暂停播放');
  },
  onEnded: () => {
    console.log('播放结束');
  },
  onError: (error) => {
    console.error('播放错误:', error);
  }
});

// 播放音频
await player.play('https://example.com/audio.mp3');

// 暂停播放
player.pause();

// 停止播放
player.stop();

// 设置音量(0-1)
player.setVolume(0.5);

// 设置播放进度(秒)
player.setCurrentTime(30);

// 销毁播放器
player.destroy();

简单切换播放

import { togglePlayAudio } from 'yy-audio-player';

// 切换播放/暂停
const player = await togglePlayAudio('https://example.com/audio.mp3');

在 Vue 组件中使用

<script setup>
import { onMounted, onUnmounted } from 'vue';
import { createAudioPlayer } from 'yy-audio-player';

let player = null;

onMounted(() => {
  player = createAudioPlayer({
    onPlay: () => {
      console.log('播放中');
    },
    onEnded: () => {
      console.log('播放完成');
    }
  });
});

onUnmounted(() => {
  if (player) {
    player.destroy();
  }
});

async function handlePlay(url) {
  await player.play(url);
}
</script>

API

createAudioPlayer(options)

创建音频播放器实例。

参数:

  • options (object) - 配置选项
    • onPlay (Function) - 播放回调
    • onPause (Function) - 暂停回调
    • onEnded (Function) - 播放结束回调
    • onError (Function) - 错误回调
    • onTimeUpdate (Function) - 时间更新回调
    • onLoadedMetadata (Function) - 元数据加载回调

返回值:

  • AudioPlayer - 音频播放器实例

AudioPlayer 类

方法

  • play(url) - 播放音频(如果相同URL则切换播放/暂停)
  • pause() - 暂停播放
  • stop() - 停止播放
  • setVolume(volume) - 设置音量(0-1)
  • setCurrentTime(time) - 设置播放进度(秒)
  • getCurrentTime() - 获取当前播放时间(秒)
  • getDuration() - 获取总时长(秒)
  • destroy() - 销毁音频对象

togglePlayAudio(url, options)

简单的播放/暂停切换函数。

参数:

  • url (string) - 音频URL
  • options (object) - 配置选项

返回值:

  • Promise<AudioPlayer> - 音频播放器实例

特性

  • ✅ 自动切换播放/暂停
  • ✅ 事件回调支持
  • ✅ 音量控制
  • ✅ 进度控制
  • ✅ TypeScript 支持