https://github.com/video-dev/can-autoplay
The auto-play feature detection in HTMLMediaElement (<audio> or <video>).
https://github.com/video-dev/can-autoplay
autoplay detection htmlmediaelement
Last synced: 4 months ago
JSON representation
The auto-play feature detection in HTMLMediaElement (<audio> or <video>).
- Host: GitHub
- URL: https://github.com/video-dev/can-autoplay
- Owner: video-dev
- License: mit
- Created: 2017-11-16T16:19:12.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-04-19T12:49:12.000Z (over 2 years ago)
- Last Synced: 2025-06-05T06:54:15.460Z (4 months ago)
- Topics: autoplay, detection, htmlmediaelement
- Language: JavaScript
- Homepage: https://video-dev.github.io/can-autoplay/
- Size: 322 KB
- Stars: 302
- Watchers: 11
- Forks: 33
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# can-autoplay.js
The auto-play feature detection in HTMLMediaElement (`` or ``).

[Demo page](https://video-dev.github.io/can-autoplay/)
Table of contents:
- [Installation](#installation)
- [Files](#files)
- [API](#api)
- [`audio(options)`](#audiooptions)
- [`video(options)`](#videooptions)
- [Example](#example)
- [Media](#media)
- [Implementation Details](#implementation-details)## Installation
```
npm install can-autoplay
```## Files
Build files are available in the `build/` directory. Bundlers will choose get the correct file chosen for them but if you just want to include it on the page, grab the `build/can-autoplay.js` file.
## API
### `audio(options)`
Parameters:
- options.inline ``, check if auto-play is possible for an inline playback, default value is `false`
- options.muted ``, check if auto-play is possible for a muted content
- options.timeout ``, timeout for a check, default value is `250` msReturns:
- ``, resolves to a ``:
- `result `, `true` - if auto-play is possible
- `error `, internal or timeout Error object```js
canAutoplay.audio().then(({result}) => {
if (result === true) {
// Can auto-play
} else {
// Can not auto-play
}
})
```### `video(options)`
Parameters:
- options.inline ``, check if auto-play is possible for an inline playback, default value is `false`
- options.muted ``, check if auto-play is possible for a muted content
- options.timeout ``, timeout for a check, default value is `250` msReturns:
- ``, resoles to a ``:
- `result `, `true` - if auto-play is possible
- `error `, internal or timeout Error object```js
canAutoplay.video().then(({result}) => {
if (result === true) {
// Can autoplay
} else {
// Can not autoplay
}
})
```## Example
```js
import canAutoPlay from 'can-autoplay';canAutoPlay
.video({timeout: 100, muted: true})
.then(({result, error}) => {
if(result === false){
console.warn('Error did occur: ', error)
}
})
```## Media
- `audio.mp3`. Created by Weston Ruter (@westonruter). Smallest possible (<0.000001 seconds long) audio file.
- `video.mp4`. Source: https://github.com/mathiasbynens/small## Implementation Details
If it's required to have a legacy browser support you could use latest `v2.x.x` version of the library.