v-zhzhou / can-autoplay

The auto-play feature detection in HTMLMediaElement (<audio> or <video>).

Home Page:https://video-dev.github.io/can-autoplay/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

can-autoplay.js

The auto-play feature detection in HTMLMediaElement (<audio> or <video>).

FileSize Version

Demo page

Table of contents:

Installation

npm install can-autoplay

API

audio(options)

Parameters:

  • options.muted <Boolean>, check if auto-play is possible for a muted content
  • options.timeout <Number>, timeout for a check, default value is 250 ms

Returns:

  • <Promise>, resoles to a <Object>:
    • result <Boolean>, true - if auto-play is possible
    • error <Error>, internal or timeout Error object
canAutoplay.audio().then(({result}) => {
  if (result === true) {
    // Can auto-play
  } else {
    // Can not auto-play
  }
})

video(options)

Parameters:

  • options.muted <Boolean>, check if auto-play is possible for a muted content
  • options.timeout <Number>, timeout for a check, default value is 250 ms

Returns:

  • <Promise>, resoles to a <Object>:
    • result <Boolean>, true - if auto-play is possible
    • error <Error>, internal or timeout Error object
canAutoplay.video().then(({result}) => {
  if (result === true) {
    // Can autoplay
  } else {
    // Can not autoplay
  }
})

Example

import canAutoPlay from 'can-autoplay';

canAutoPlay
    .video({timeout: 100, muted: true})
    .then(({result, error}) => {
        if(result === false){
            console.warn('Error did occur: ', error)
        }
    })

About

The auto-play feature detection in HTMLMediaElement (<audio> or <video>).

https://video-dev.github.io/can-autoplay/

License:MIT License


Languages

Language:JavaScript 87.6%Language:HTML 12.4%