question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Monitoring Volume Changes on Wistia player.

See original GitHub issue

I couldn’t find a way to bind the volume changes in React player or the stand alone wistia player. I’m not sure if this will work for other players so I just made a minor change a local copy of the Wistia player.

I modified the Wistia.js component

I used the local storage to volume change by default (versus via a prop) since I didn’t want to modify the code too much. Below shows the changes and below that, a full example of binding the ‘videochange’ event to react-player.

  load (url) {
    const _this = this
    const currentVolume = window.localStorage.getItem('currentVolume') || 1
   // 
        options: {
         //
          volume: currentVolume,
          //
        },
        onReady: player => {
         ///
          this.player.bind('volumechange', _this.onVolumeChange)
          onReady()
        }
      })
    })
  }
  onVolumeChange = (event) => {
      window.localStorage.setItem('currentVolume', event)
  }

Full Example

I’m putting this as a simple way to copy and paste to your local project.

import React, { Component } from 'react'

import { callPlayer, getSDK } from 'react-player/lib/utils'
import createSinglePlayer from 'react-player/lib/singlePlayer'

const SDK_URL = '//fast.wistia.com/assets/external/E-v1.js'
const SDK_GLOBAL = 'Wistia'
const MATCH_URL = /(?:wistia\.com|wi\.st)\/(?:medias|embed)\/(.*)$/

export class Wistia extends Component {
  static displayName = 'Wistia'
  static canPlay = url => MATCH_URL.test(url)
  static loopOnEnded = true

  callPlayer = callPlayer
  getID (url) {
    return url && url.match(MATCH_URL)[1]
  }
  load (url) {
    const _this = this
    const currentVolume = window.localStorage.getItem('currentVolume') || 1
    const { playing, muted, controls, onReady, onPlay, onPause, onSeek, onEnded, config } = this.props
    getSDK(SDK_URL, SDK_GLOBAL).then(() => {
      window._wq = window._wq || []
      window._wq.push({
        id: this.getID(url),
        options: {
          autoPlay: playing,
          silentAutoPlay: 'allow',
          muted: muted,
          controlsVisibleOnLoad: controls,
          volume: currentVolume,
          ...config.wistia.options
        },
        onReady: player => {
          this.player = player
          this.player.bind('play', onPlay)
          this.player.bind('pause', onPause)
          this.player.bind('seek', onSeek)
          this.player.bind('end', onEnded)
          this.player.bind('volumechange', _this.onVolumeChange)
          onReady()
        }
      })
    })
  }

  onVolumeChange = (volumeLevel) => {
      window.localStorage.setItem('currentVolume', volumeLevel)
  }
  play () {
    this.callPlayer('play')
  }
  pause () {
    this.callPlayer('pause')
  }
  stop () {
    this.callPlayer('remove')
  }
  seekTo (seconds) {
    this.callPlayer('time', seconds)
  }
  setVolume (fraction) {
    this.callPlayer('volume', fraction)
  }
  mute = () => {
    this.callPlayer('mute')
  }
  unmute = () => {
    this.callPlayer('unmute')
  }
  setPlaybackRate (rate) {
    this.callPlayer('playbackRate', rate)
  }
  getDuration () {
    return this.callPlayer('duration')
  }
  getCurrentTime () {
    return this.callPlayer('time')
  }
  getSecondsLoaded () {
    return null
  }
  render () {
    const id = this.getID(this.props.url)
    const className = `wistia_embed wistia_async_${id}`
    const style = {
      width: '100%',
      height: '100%'
    }
    return (
      <div key={id} className={className} style={style} />
    )
  }
}

export default createSinglePlayer(Wistia)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
thanhnx9368commented, Sep 28, 2022

@cookpete It’s working. 🥳🥳🥳 Thank you for supporting!

1reaction
cookpetecommented, Sep 28, 2022

@thanhnx9368 Sorry, I thought we were talking about Wistia videos. If you are playing files we should discuss this in a new issue, but you can still check the muted status of the file player:

internalPlayer.addEventListener('volumechange', function () {
    console.log('muted', internalPlayer.muted);
}, false);
// video.onvolumechange = ... works too

Adapted from https://stackoverflow.com/a/25106820

Read more comments on GitHub >

github_iconTop Results From Across the Web

Monitoring Volume Changes on Wistia player. #524 - GitHub
I used the local storage to volume change by default (versus via a prop) since I didn't want to modify the code too...
Read more >
JavaScript Player API — Support - Wistia
The Wistia library will monitor the DOM for changes like this, and automatically embed a video where it sees an element with the...
Read more >
Updating the Player Controls — Support - Wistia
Volume Control. This button allows a viewer to change the volume of your video. To adjust the volume, hover over the icon, and...
Read more >
Embed Options and Plugins — Support - Wistia
When set to true , the video will monitor the width of its parent element. When that width changes, the video will match...
Read more >
The Wistia Guide to Video Metrics
Using play rate to measure relevance · Move the video to a more prominent location on the page, or make the embed larger....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found