【问题标题】:How to identify volume change in youtube player如何识别 youtube 播放器中的音量变化
【发布时间】:2015-07-16 03:00:40
【问题描述】:

我正在使用“angualr-youtube-embed”指令将 youtube 播放器嵌入到我的 Angular Web 应用程序中。在那我必须识别播放和暂停和音量变化事件。要收听播放和暂停事件,我使用下面给出的代码。

 $scope.$on('youtube.player.playing', function ($event, player) {
    // to do functions when the video is playing.
  });

  $scope.$on('youtube.player.paused', function ($event, player) {
    // to do functions when the video is paused.
  });

现在我的要求是,我想在 youtube 播放器中更改音量时做一些工作,我需要识别该音量更改事件。但我不知道如何在 youtube 播放器中收听音量变化。我该如何解决这个问题?

提前致谢。

【问题讨论】:

    标签: javascript angularjs youtube youtube-iframe-api


    【解决方案1】:

    以防万一有人有同样的问题,here 是我的完整答案。

    今天的代码如下:

    setInterval(this.getChangedVolume, 250)
    
    getChangedVolume () {
      let currentYoutubeVolume = this.player.getVolume()
      // Do some things, for example (will show Promise):
      // console.log(currentYoutubeVolume)
    
      // YouTube returns Promise, but we need actual data, so:
      // Promise.resolve(currentYoutubeVolume).then(data => { this.volumeLv = data })
      }
    

    【讨论】:

      【解决方案2】:
      player.getVolume():Number
      

      返回播放器的当前音量,一个介于 0 和 100 之间的整数。请注意,即使播放器静音,getVolume() 也会返回音量。

      更多详情请查看:YouTube Player Controls

      【讨论】:

      • 感谢您的评论。实际上我需要在音量改变时触发一个函数,因为我必须识别音量改变事件。
      • 您需要将当前音量保存在某个变量中,并在音量更改时将该音量保存在其他变量中并比较这些变量(如果不同,您可以根据音量更改进行操作。
      • 其实我正在寻找一个事件监听器。但现在你节省了我的时间。谢谢贾敏。
      • Youtube api 中没有用于音量变化的事件监听器。如果有用,请不要忘记将此答案标记为有用。
      猜你喜欢
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 2020-07-17
      • 2018-06-02
      • 2014-09-12
      相关资源
      最近更新 更多