Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nondanee/remoteaudioelement
HTMLAudioElement for Node.js
https://github.com/nondanee/remoteaudioelement
audio-library web-audio-api
Last synced: 7 days ago
JSON representation
HTMLAudioElement for Node.js
- Host: GitHub
- URL: https://github.com/nondanee/remoteaudioelement
- Owner: nondanee
- License: mit
- Created: 2019-06-02T15:49:53.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-05T02:53:47.000Z (over 3 years ago)
- Last Synced: 2023-03-03T07:48:39.892Z (over 1 year ago)
- Topics: audio-library, web-audio-api
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RemoteAudioElement
这是一个为 Node.js 提供 HTMLAudioElement 接口的包
*Powered by Puppeteer & Headless Chrome*
## About
想做一个 CLI 播放器,但是折腾了一圈最后还是发现 Windows, Linux, macOS 全平台兼容性 / 可控性最好的播放 (在线) 音乐方式是 `new Audio(url).play()`。木的办法,想了想如果要让可执行程序最小化的话只能通过调用本地浏览器了
使用 puppeteer 的 `page.evaluate()` 和 `page.exposeFunction()` 调用页面内的方法并传递监听事件,再用 Proxy 加一个兼容层,提供和 Audio Object 一致的 API
## Usage
> 支持 `PUPPETEER_EXECUTABLE_PATH` 变量,支持寻找默认安装位置,支持 `PATH` 变量 (优先级递减)
```sh
$ npm install nondanee/RemoteAudioElement
``````javascript
const Audio = require('remote-audio-element');(async () => {
let audio = await new Audio()audio.ontimeupdate = info => {
console.log('currentTime', info.currentTime)
}audio.addEventListener('loadedmetadata', info => {
console.log('duration', info.duration)
})audio.on('ended', () => {
audio.dispose()
})await (audio.src = 'https://dl-web.dropbox.com/s/mzxthflt1kumqs1/BigBowlThickNoodle.mp3')
await audio.play()
console.log(await audio.buffered)
})()
```## Documentation
> 具体请参考 HTMLAudioElement 的 MDN 文档
### Properties
`audioTracks`, `autoplay`, `buffered`, `crossOrigin`, `currentSrc`, `currentTime`, `defaultMuted`, `defaultPlaybackRate`, `duration`, `ended`, `error`, `loop`, `mediaGroup`, `muted`, `networkState`, `paused`, `playbackRate`, `played`, `preload`, `readyState`, `seekable`, `seeking`, `src`, `startDate`, `volume`
### Methods
`canPlayType`, `load`, `play`, `pause`, `fastSeek`, `dispose`
### Events
`abort`, `canplay`, `canplaythrough`, `durationchange`, `emptied`, `ended`, `error`, `loadeddata`, `loadedmetadata`, `loadstart`, `pause`, `play`, `playing`, `progress`, `ratechange`, `seeked`, `seeking`, `stalled`, `suspend`, `timeupdate`, `volumechange`, `waiting`
### Extra
set / get properties 以及 apply methods 均为 `Promise`,需要 `await` 等待调用完成;add event listener 不需要
因事件监听需要获得实时状态等不及 `await` 而做了优化,监听的事件的 event 接口已包含 audio 的所有属性值
添加 events 监听支持 Node.js EventEmitter 的所有方法,支持页面内的事件侦听器 (通过 `addEventListener` 注册函数) 和事件处理器 (通过 `on...` 属性注册函数)
`seekable`, `buffered`, `played` 属性返回的 TimeRanges 对象被序列化为 `[{start: 0, end: 100}]` 形式的 JSON object
调用 `audio.dispose()` 可以显式关闭 browser
## Reference
- [GoogleChrome/puppeteer](https://github.com/GoogleChrome/puppeteer)
- https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement
- https://www.w3schools.com/tags/ref_av_dom.asp## License
MIT