Youtube iFrame API setPlaybackQuality ignored on mobile device
Asked Answered
P

2

15

I'm trying to setup YouTube iframe API to play a FullHD video with a lower quality. My goal is to save bandwidth on mobile devices and reduce loading time. My HTML structure is the classical player div, plus a debug div for messages.

HTML

<div id="debug"></div>

<div id="your_video_id">
  <div id="player"></div>
</div>

I've tried to invoke setPlaybackQuality as soon as the player is ready, to avoid mobile users wasting time in buffering (as suggested in this post). I've also invoked it in both "BUFFERING" and "PLAYING" states. When quality changes, debug content is updated with actual playback quality.

JAVASCRIPT

/* Trigger player ready */
function onPlayerReady(event) 
{
    player.setPlaybackQuality("small");
}

/* Detect playback quality changes */
function onQualityChange(event) 
{
    document.getElementById("debug").innerHTML = event.data;
}

/* Trigger player events */
function onPlayerStateChange(event) 
{
    if (event.data == YT.PlayerState.BUFFERING) 
    {
        player.setPlaybackQuality("small");
    }

    if (event.data == YT.PlayerState.PLAYING) 
    {
        player.setPlaybackQuality("small");
    }
}

The code seems to work on desktop (debug is correctly set to "small"), but it's ignored on mobile (debug set to "large", tested with Android 4.2.2). Is there a solution for this?

Pierpont answered 19/12, 2014 at 8:23 Comment(9)
+1. I get the same issue, have tried it in onReady, onStateChange with BUFFERING and PLAYING, all separately and in various combinations -- none of it works. I filed this issue: code.google.com/p/gdata-issues/issues/…Pernas
On iOS the video quality is set directly based on the size of the iFrame player. So no matter what size you set, the mobile player will always override your setting to match whatever the closest size is to the player size. This is a huge problem with the player helper YouTube provides to iOS developers (which is really just the iFrame player in a web view). YouTube claims this is done purposely to avoid unnecessary data usage on mobile.Compressed
@JAL: Hm. So, then, I have it near the minimum size specified - about 350 x 200. Shouldn't it be trying to set it to 240p? And since it's actually 175 x 100 \@2x, wouldn't it be closer to 144p? I think this is an issue on the YT iFrame API side because I have also noticed on the desktop seeking to lower quality doesn't work but seeking to higher quality does.Pernas
The iFrame API doesn't work right on mobile, period. Considering that it is the next API up for deprecation (and is subject to deprecation at any moment), I doubt Google is working on fixing all of the issues associated with the iFrame player on mobile.Compressed
@JAL: Huh. Is there any alternative to the iFrame API that will allow developers to integrate the YouTube platform into their application after it is deprecated?Pernas
For web, I think Google is going to try to push their HTML5 player. On Android, Google provides the Android Player API. On iOS, Google provides the YTPlayerView (which is just the iFrame player in a UIWebView).Compressed
@JAL: I see. Could you provide me somewhere I can find more info about their HTML5 player for web? For example, is there an API through which one can interact with it?Pernas
I thought there were HTML5 API docs but I can't seem to find them. Let me dig around and see what I can find.Compressed
Let us continue this discussion in chat.Compressed
P
6

The workaround I found is to create the player with no videoId, never call setPlaybackQuality anywhere else, but call loadVideoById(videoId, 0, desiredQuality) inside of your onReady handler. This was respected on both Android and iOS when tested, calling onPlaybackQualityChanged with the correct quality once on Android and Desktop and twice on iOS, never changing the quality to anything higher or lower.

Unfortunately, if you pass any quality that isn't available on the video, it will force 'medium'. Be aware of that. Also, this is unlikely to break any quality-capping that the API does on iOS to prevent extraneous data usage on 3G/4G/LTE networks.

Pernas answered 28/5, 2015 at 16:4 Comment(7)
I followed your steps but it's not working for me. Do you know if the API's behavior has changed since your answer?Maihem
Have not checked recently. Which platform are you having trouble on, and are you absolutely sure you are using loadVideoById (not cueVideoById or similar) with the 3 arguments, never calling setPlaybackQuality, and the video you are playing actually has the playback quality you are trying to set? Which quality is it actually setting?Pernas
Yeah I am using loadVideoById. I am trying Chrome on both Android and iOS. No matter what I do, I always get quality medium on my iPhone 5, and large on my Nexus 5. The exact same code works just fine on desktop browsers and I can change the quality.Maihem
Ah, I've only tested using WebView (Android) and UIWebView/WKWebView (iOS), not sure if that would affect it. Also the last time I tested this was probably ~6 months ago, so YMMV. Sorry to hear it doesn't work and let me know if you find a workaround!Pernas
Thanks for the info :) I will work more on this over the weekend and will definitely let you know if I find a workaround.Maihem
@Maihem did you find a solution? I have the same problem with iPhone 6s and any simulators (iPhone 5-10, xr, xs). It always selects medium quality, only official YouTube app allows to change quality: #56030015Risner
Nope :( and I stopped looking into this 3 years ago, so I am not really up to date.Maihem
K
3

It's 2020, and YouTube has deprecated the use of the setPlaybackQuality function.

There is currently no way to force the playback quality of an embedded video. Even setting the player to 144px width doesn't seem to work anymore.

Katrinakatrine answered 12/3, 2020 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.