项目作者: hawtim

项目描述 :
A audio library for applications.
高级语言: TypeScript
项目地址: git://github.com/hawtim/suono.git
创建时间: 2020-10-10T02:55:21Z
项目社区:https://github.com/hawtim/suono

开源协议:

下载


Suono

Suono is a simple native audio lib for music application, it compatible with android native browser.

Easy to extend and welcome PR and issues. prs

npm
node
size
npm-l
npm-d
licenses

Installation

  1. npm i suono
  2. # or using yarn
  3. yarn add suono

Started

  1. import { Suono } from 'suono'
  2. const suono = new Suono({
  3. autoplay: false,
  4. controls: false,
  5. preload: 'metadata',
  6. fallback: 'Your browser doesn\'t support HTML5 audio.',
  7. autoSkip: true,
  8. volume: 1,
  9. mode: 'order',
  10. debug: false,
  11. crossorigin: 'anonymous'
  12. })
  1. <script type="module">
  2. import { Suono } from '../dist/index.esm.js'
  3. const suono = new Suono({
  4. mode: 'order' // default value
  5. })
  6. </script>

Feature

  • Support singleton with globally shared and multiple instaces
  • Support Publish-Subscribe, listen to audio events and can set callback to provide fully controlled
  • Have four play mode inside, order, list, single, random and also custom mode
  • Support multiple audio file extenstion and allow to fallback

Options

src: string | string[]

return the current source of the audio

debug: boolean

for debugging

autoplay: boolean

set auto play or not, according to the browser autoplay strategy

preload: string

set the native preload type, such as none, metadata, auto. Default value is metadata

loop: boolean

set loop, default false

controls: boolean

set true to show the native controls for audio tag

volume: number

set the range of volume, range from 0 to 1

crossorigin: string

if face the croessorigin issue, you can set the crossorigin type: anonymous or use-credentials

Instance properties

name: string

duration: number

fallback: string

loading: boolean

if the source is loading, will return true

playList: ListItem[]

the playlist from the instance

currentIndex: number

the current index in the playlist

mode: string

the playmode,'order', 'single', 'shuffle', 'list'

autoSkip: boolean

set auto skip to next audio when encounter errors or something else

API

init({ src, name }: ListItem)

create the audio in the memory and register the hooks for events, and load the resources.

load()

reset the media element and load the resources, play from the start

play()

play the audio

pause()

pause the audio

seek(target: number)

seek to the specific timing of the audio

skipTo(listItem: ListItem) / switch({ name, src }: ListItem)

skip to the other item in the playlist and play

prev() && next()

play the previous or the next item in the playlist, will have different behavior accord to different play mode

appendChild()

append the audio element before the end tag of body

removeChild()

remove the audio element from the end tag of body

destroy()

destroy the audio instance and element

getName(): string

get current item name

getSrc(): string

get current item src

getCurrentSrc(): string

get current item sources, for multiple source item

getCurrentTime(): number

getPlayList(): ListItem[]

updateLoop(status: boolean)

updateName(name: string, src: string)

updateLoading(status: boolean)

updateDuration(duration: number)

updatePlayMode(mode: string)

updatePlayList(list: ListItem[])

Develop

  1. "scripts": {
  2. "prebuild": "del-cli dist",
  3. "esm": "tsc --module esnext && cpy dist/index.js dist --rename index.esm.js",
  4. "cjs": "tsc --module commonjs",
  5. "build": "npm run esm && npm run cjs",
  6. "dev": "npm run build --watch",
  7. }

License

MIT