YouTube iFrame API “setPlaybackQuality” or “suggestedQuality” not working

后端 未结 6 1054
温柔的废话
温柔的废话 2020-11-30 07:02

I\'m having some trouble setting the quality settings on a video via the Youtube iFrame API. This is my code:

var player;

player = new YT.Player(\'player\',         


        
6条回答
  •  北海茫月
    2020-11-30 07:14

    I have the exact same problem and workaround. I think what's happening is that YouTube is only allowing quality levels based on the actual size of the display, so unless you have your video 720px tall you can't default to 720p before it's actually playing. Then the user controls kick in and YouTube stops being a dick.


    EDIT

    Just hit a breakthrough: If you use event 3 (buffering) instead of event 5 (playing) there's no stutter for the user. Quality is changed as soon as it starts loading. Only weird thing is you need to set it in onPlayerReady as well or it doesn't work.

    function onPlayerReady(event) {
        event.target.setPlaybackQuality('hd720');
    }
    function onPlayerStateChange(event) {
        if (event.data == YT.PlayerState.BUFFERING) {
            event.target.setPlaybackQuality('hd720');
        }
    }
    

提交回复
热议问题