嵌入式YouTube视频启动时如何停止播放音乐? [英] How to stop music when embedded youtube video starts?

查看:255
本文介绍了嵌入式YouTube视频启动时如何停止播放音乐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML网站,开始播放音乐,但是当用户点击嵌入我网站的YouTube视频播放时,我希望它停止播放..所以它会使背景音乐和视频音乐不再存在能够同时运行,因为这很烦人。

I have an HTML site with which music starts on entering, but I want it to stop when the user clicks play on the youtube video that is embedded onto my website.. so it makes the background music and the video music not be able to run simultaneously because that's annoying.

有关如何做到这一点的任何想法?或者是否有解决此问题的方法?

Any idea on how to do that? or if there is any solution for this problem?

推荐答案

检测玩家的状态变化( onStateChange event)使用JavaScript: https://developers.google.com/youtube/js_api_reference #Events

Detect the player's state change (onStateChange event) using JavaScript: https://developers.google.com/youtube/js_api_reference#Events

如果使用 iframe 加载视频(如评论所示),请参阅< a href =https://developers.google.com/youtube/iframe_api_reference =nofollow> YouTube指南 iframe 嵌入(请注意代码检测玩家事件的样本。)

If the video is loaded using an iframe (as the comments indicate) see Guide for YouTube iframe embeds (note the code sample which detects player events).

示例

  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'u1zgFlCw8Aw',
      events: {
        'onStateChange': onPlayerStateChange
      }
    });
  }

  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING) {
      // code to stop the audio player should probably go here
    }
  }

播放器启动时,使用JavaScript停止音频。你没有告诉我们它是如何嵌入到页面中的,但几乎所有玩家都可以用脚本停止(甚至 bgsound 也可以被黑客攻击)。

When the player starts, use JavaScript to stop the audio. You haven't told us how it is embedded into the page, but nearly any player can be stopped with script (even bgsound can be hacked).

并鼓励您的客户在页面加载时不自动播放音乐。从纯粹的商业角度来看,它可能会让您的客户付出代价。

And encourage your client to not auto-play music when the page loads. From a pure business standpoint, it can cost you customers.

这篇关于嵌入式YouTube视频启动时如何停止播放音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆