setPlaybackQuality不改变质量

时间:2018-01-15 19:27:43

标签: youtube-iframe-api

我正在尝试使用iframe / javascript API更改Youtube视频的播放质量。

然而,API似乎忽略了我的请求。 这是一个简单的例子:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div id="player"></div>

    <script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');

      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '360',
          width: '640',
          videoId: 'M7lc1UVf-VE',
          events: {
            'onReady': onPlayerReady,
            'onPlaybackQualityChange': onPlaybackQualityChange
          }
        });
      }
      function changeQuality() {
        var qualities = player.getAvailableQualityLevels();
        var actual = player.getPlaybackQuality();
        for (var i = 0; i < qualities.length; i += 1) {
          if (actual !== qualities[i]) {
            console.log('Changing quality to:' + qualities[i]);
            //onPlaybackQualityChange is never called!
            player.setPlaybackQuality(qualities[i]);
            break;
          }
        }
      }
      function onPlayerReady(event) {
        event.target.playVideo();
        setTimeout(changeQuality, 1000);
      }
      function onPlaybackQualityChange(event) {
        console.log('Current quality:' + event.data);
      }
    </script>
  </body>
</body>
</html>

如果您运行代码,您可以看到onPlaybackQualityChange仅在加载视频时被调用,并且完全忽略使用setPlaybackQuality()更改视频质量的请求。 任何帮助表示赞赏。感谢。

3 个答案:

答案 0 :(得分:1)

不幸的是,let stack = UIStackView() stack.spacing = 10 stack.distribution = .fill stack.alignment = .fill stack.axis = .vertical contentView.addSubview(stack) stack.addArrangedSubview(childView1) stack.addArrangedSubview(childView2) childView1.translatesAutoresizingMasks = false childView2.translatesAutoresizingMasks = false NSLayoutConstraint.activate([ contentView.topAnchor.constraint(equalTo: stack.topAnchor), contentView.leadingAnchor.constraint(equalTo: stack.leadingAnchor), contentView.trailingAnchor.constraint(equalTo: stack.trailingAnchor), contentView.bottomAnchor.constraint(equalTo: stack.bottomAnchor), // setting their heights using constraints childView1.heightAnchor.constraint(equalToConstant: 200), childView2.heightAnchor.constraint(equalToConstant: 200), ]) 功能至少在几个月内没有起作用。

出于测试目的,请尝试使用setPlaybackQuality(),您会发现可以毫无问题地设置播放速率,但不能保证质量。

答案 1 :(得分:-1)

player.loadVideoById({&#39; videoId&#39;:&#39; M7lc1UVf-VE&#39;,                             &#39; startSeconds&#39;:parseInt(5),                             &#39;建议质量&#39;:&#39; hd720&#39;});

答案 2 :(得分:-1)

您必须使用setPlaybackQualityRange来设置Youtube质量。

在此处查看有关setPlaybackQuality https://developers.google.com/youtube/iframe_api_reference#setPlaybackQuality

的更多信息