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

@dino-dev/vite-plugin-use-sound

v1.0.2

Published

A vite plugin for playing sound effects

Downloads

85

Readme

vite-plugin-use-sound

中文 | English

用于将声音文件生成为 base64 url 数据。

功能

  • 预加载 所有声音在项目运行时生成,只需播放即可。
  • 高性能 内置缓存,仅在文件修改时重新生成。

安装 (使用 yarn 或 npm)

node 版本: >=12.0.0

vite 版本: >=4.0.0

yarn add vite-plugin-use-sound -D
# or
npm i vite-plugin-use-sound -D
# or
pnpm install vite-plugin-use-sound -D

使用方法

在 vite.config.ts 中配置插件

import { createUseSoundPlugin } from 'vite-plugin-use-sound'
import path from 'path'

export default () => {
  return {
    plugins: [
      createUseSoundPlugin({
        // Specify the sound folder to be convert
        soundDirs: [path.resolve(process.cwd(), 'src/assets/sound')],
        // Specify symbolId format
        symbolId: 'sound-[dir]-[name]',

      }),
    ],
  }
}

src/main.ts中设置 [Optional]

import { setupUseSound } from 'virtual:use-sound'

// 使用 `uni.createInnerAudioContext` 作为播放器创建器的 useSound 设置
setupUseSound((url: string) => {
  const player = uni.createInnerAudioContext()
  player.src = url
  player.autoplay = false

  player.onPlay(() => {
    console.log('音频开始播放')
  })

  player.onError((error: any) => {
    console.error('音频播放出错:', error)
  })
  return player
})

注意:

如果你不调用 setupUseSound,默认的播放器是 new Audio()

在组件中使用

声音目录结构

# src/sound

- option-error.mp3
- option-success.wav
- sound3.m4a
- dir/sound4.aac

Vue 方式

/src/pages/Home.vue

<template>
  <button @click="playSuccess()">play</button>
</template>

<script lang='ts' setup>
import { useSound } from 'virtual:use-sound'

// 错误提示音
const errorSound = useSound('sound-option-error')
errorSound.play()

// 正确提示音
const {play: playSuccess, destroy: destroySuccess} = useSound('sound-option-success')

// 声音播放器
const wordPlayer = useSound()
wordPlayer.onPlayEnded(() => {
  console.log('播放结束')
})

onUnload(() => {
  // 销毁声音特效
  errorSound.destroy()
  destroySuccess()
  wordPlayer.destroy()
})

</script>

获取所有的 SymbolId

import ids from 'virtual:use-sound-ids'
// => ['sound-option-error','sound-option-success','sound-sound3', 'sound-dir-sound4']

选项

| 参数 | 类型 | 默认值 | 描述 | |------------|--------------|-----------------------|------------------------------------------------| | soundDirs | string[] | - | 需要生成声音文件夹的路径 | | symbolId | string | sound-[dir]-[name] | 声音的 symbolId 格式,详见下方的描述 |

symbolId

sound-[dir]-[name]

[name]:

sound file name

[dir]

插件的声音不会通过哈希值进行区分,而是通过文件夹进行区分。

如果与 soundDirs 对应的文件夹包含其他文件夹,则生成的 SymbolId 将写在注释中。 例如:

# src/sound
- sound1.wav # sound-sound1
- sound2.wav # sound-sound2
- sound3.wav # sound-sound3
- dir/sound1.wav # sound-dir-sound1
- dir/dir2/sound1.wav # sound-dir-dir2-sound1

支持的声音文件格式

  • mp3
  • wav
  • m4a
  • aac

Typescript Support

如果使用 Typescript,可以在 tsconfig.json 中添加以下配置:

// tsconfig.json
{
  "compilerOptions": {
    "types": ["vite-plugin-use-sound/client"]
  }
}

Note:

尽管使用文件夹来区分它们可以在很大程度上避免重复名称的问题,但在 soundDirs 中可能会出现具有多个文件夹和相同文件名的声音。

这需要开发者自己避免。

License

MIT © Dinodev.cn