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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nicolaz/soundbox

v2.0.0

Published

A html5 Audio player core

Downloads

2

Readme

@nicolaz/soundbox

HTML5 Audio 的高级封装,并拥有可添加和控制多个音频的能力。

快速上手

安装

npm i @nicolaz/soundbox

使用

import Soundbox from '@nicolaz/soundbox';

const soundbox = new Soundbox();
const sound = soundbox.add({ src: /*AUDIO_URL*/ });
sound.play();

API

Soundbox

add( soundData )

soundData: [Object]

音频数据,比如:

{
  src: "...",
  volume: 50,
}

执行 add 方法后,返回一个 sound 对象。

remove( sound )

sound: [Sound]

通过 add 方法生成的对象。删除成功,返回 soundbox 中音频对象列表的索引,否则返回 -1

reset()

重置 soundbox,这会清理所有 addsound

destroy()

soundbox 不再使用,调用后 soundbox 不再工作。如你要重新使用,需要重新 new 一个实例。

Sound

通过 soundbox.add 返回的对象,执行 soundbox.add 时,可传入如下属性:

  • src: 音频地址
  • volume: 音频音量
  • muted: 音频是否静音
  • range[Array]: 包含 2 个时间值的数组 [start, end],表示一个音频在时间轴上可播放的范围

play()

非同一个 sound 执行 play,会触发 start 事件

pause()

非播放中的 sound 执行无效

setPosition( time )

time: [Number]|秒

设置播放进度,如果为当前播放音频,会触发 positionchange 事件。没 play 过的 sound 执行无效,你也不应该为非播放中的 sound 设置播放进度,可在外部通过 state 来判断处理。

setMute( muted )

muted: [Boolean]

设置是否静音

setVolume( volume )

volume: [Number]|0-100

Sound Events

通过 sound 对象的 on(type, handler) 方法绑定

progress

缓冲进度变化时触发

handler( bufferedProgress )

bufferedProgress: [Number]|0-1

start

新音频/音频切换,并开始播放时触发

handler( data )

data: [Object]|{ duration }

statechange

音频状态更新时触发

handler( state )

state: [String]

见下方的 STATES

positionchange

音频时间进度更改时触发

handler( currentTime, playedProgress )

currentTime: [Number]

当前播放的时间

playedProgress: [Number]|0-1

error

音频播放错误时触发

handler( Error )

STATES

直接从安装包导入:

import { STATES } from '@nicolaz/soundbox'

STATES 包含以下属性:

  • PREBUFFER: 'waiting'

  • BUFFERING: 'loadeddata'

  • PLAYING: 'playing'

  • PAUSE: 'pause'

  • STOP: 'suspend'

  • END: 'ended'

Demo

这里有一个 React 组件的示例:Demo

LICENSE

MIT © nicolaszhao