Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  168] [ 6]  / answers: 1 / hits: 29679  / 13 Years ago, tue, january 10, 2012, 12:00:00

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', {
height: '490',
width: '725',
videoId: yturl,
/* setPlaybackQuality: 'hd720', <-- DOES NOT WORK */
/* suggestedQuality: 'hd720', <-- DOES NOT WORK */
events: {
'onReady': onPlayerReady
}
});

function onPlayerReady(event) {
player.setPlaybackQuality('hd720'); // <-- DOES NOT WORK
event.target.setPlaybackQuality('hd720'); // <-- DOES NOT WORK
player.setVolume(100); // <-- DOES WORK
console.log(player.getPlaybackQuality()); // <-- Prints 'small' to console
event.target.playVideo();
}


The funny thing is that my call to player.setPlaybackQuality or event.target.setPlaybackQuality doesn't give any errors. It just looks as if the player ignores it. A call to, say, player.setSuggestedQuality (a function that doesn't exist) throws an error as expected.



I've tried all the valid string parameters as outlined in the API reference ('medium', 'large', 'hd720' etc). None of them work.



Anyone have any suggestions to how I'm supposed to set this property?


More From » api

 Answers
29

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');
}
}

[#88120] Monday, January 9, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jonmicahm

Total Points: 603
Total Questions: 120
Total Answers: 108

Location: Guam
Member since Fri, Jul 31, 2020
4 Years ago
;