iOS YTPlayerView强制视频质量 [英] iOS YTPlayerView force video quality

查看:136
本文介绍了iOS YTPlayerView强制视频质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前在我们的应用程序中使用iOS-youtube-player-helper库.有一个视图控制器,其YTPlayerView的纵横比为16:9,这意味着它仅占用屏幕的一部分.视频以中等大小加载,无论如何,我都无法以720P或1080P播放.我确信这些质量是可以使用的,只是YTPlayerView强制基于视频播放器高度的质量.因为这是一个库,而不是直接嵌入iframe,所以我无法使用"vq"参数(在playerVars中指定vq似乎不起作用),并且将质量设置为较小然后再更改它也不起作用(请参阅此问题在GitHub上)

I am currently using iOS-youtube-player-helper library in our application. There is a view controller, with a YTPlayerView that has an aspect ratio of 16:9, which means it takes only a part of the screen. The video is loaded in medium and no matter how, I could not get it to play in 720P or 1080P. I am certain that these qualities are available, it's just the YTPlayerView forcing the quality based on the video player height. Because this is a library and not direct iframe embed, I cannot use "vq" parameter(specifying vq in playerVars does not seem to work), and setting the quality to be small then change it later does not work either(refer to this issue on GitHub)

现在,由于UI设计问题,考虑到我无法制作YTPlayerView来填充整个屏幕的因素,那么,是否可以强制YTPlayerView至少以720P播放? (解决方法,更改库代码,...)

Now, given the factor that I cannot make the YTPlayerView to fill up the whole screen, because of UI designing issues. So, is it possible to force the YTPlayerView to play in at least 720P? (Workarounds, changing the library code, ...)

因为这是一个将在App Store上发布的应用(当然,我们也不想与Google发生任何法律纠纷),所以请不要建议使用与Youtube ToC相违背的库,例如

Because this is an app that will be on App Store(and of course we don't want to have any legal disputes with google either), please don't suggest using libraries that are against the Youtube ToC such as XCDYouTubeKit

非常感谢

推荐答案

我找到了一种解决方法,对我来说很好.

I've found a workaround and this works well for me.

首先,问题取决于YTPlayerView内部构造的webView大小.例如,如果您有一个320x200的playerView,请尝试将视频强行设置为720h,因为iFrame youtube播放器类会根据您的播放器尺寸重新切换为更好的分辨率(在这种情况下,质量较低,因为您拥有320x200).

First of all, the problems depends by the webView size constructed inside the YTPlayerView. For example if you have a 320x200 playerView, try to forcing your video to 720hd don't work because the iFrame youtube player class re-switch to a better resolution according to your player size (in this case small quality because you have 320x200).

您可以看到 SO答案来解释此问题.

You can see this SO answer that explain this issue.

YTPlayerView类导入到项目中后,将有两个文件:YTPlayerView.hYTPlayerView.m

When you have imported the YTPlayerView class to your project you have two files: YTPlayerView.h and YTPlayerView.m

YTPlayerView.m ( 更新为在iPad上也可以使用 )

YTPlayerView.m (Update to work also on iPads)

我更改了使用自定义尺寸(4k分辨率)初始化Webview的功能,最后,我增加了缩放内容和恢复原始帧的可能性,如下所示:

I've change the function where the webview is initialized with a custom size (4k resolution) and to the last part I've added the possibility to scale the contents and restore the original frame, like this:

- (UIWebView *)createNewWebView {
    CGRect frame = CGRectMake(0.0, 0.0, 4096.0, 2160.0); //4k resolution
    UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
    //UIWebView *webView = [[UIWebView alloc] initWithFrame:self.bounds];

    webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    webView.scrollView.scrollEnabled = NO;
    webView.scrollView.bounces = NO;

    if ([self.delegate respondsToSelector:@selector(playerViewPreferredWebViewBackgroundColor:)]) {
        webView.backgroundColor = [self.delegate playerViewPreferredWebViewBackgroundColor:self];
        if (webView.backgroundColor == [UIColor clearColor]) {
            webView.opaque = NO;
        }
    }

    webView.scalesPageToFit = YES;
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
    {
        CGSize contentSize = webView.scrollView.contentSize;
        CGSize viewSize = self.bounds.size;
        float scale = viewSize.width / contentSize.width;
        webView.scrollView.minimumZoomScale = scale;
        webView.scrollView.maximumZoomScale = scale;
        webView.scrollView.zoomScale = scale;
        // center webView after scaling..
        [webView setFrame:CGRectMake(0.0, self.frame.origin.y/3, 4096.0, 2160.0)];
    } else {
        webView.frame = self.bounds;
    }
    [webView reload];
    return webView;
}


希望这对尝试在项目中使用Youtube原始播放器的人有所帮助.


Hope this helps who try to use the Youtube original player in his project.

PS :我所有的尝试都是通过一个快速项目和以下变量进行的:

P.S.: All my tries were did with a swift project and the following vars:

var playerVars:Dictionary =
        ["playsinline":"1",
         "autoplay":"1",
         "modestbranding":"1",
         "rel":"0",
         "controls":"0",
         "fs":"0",
         "origin":"https://www.example.com",
         "enablejsapi":"1",
         "iv_load_policy":"3",
         "showinfo":"0"] 

使用功能:

self.playerView.load(withVideoId: "Rk6_hdRtJOE", playerVars: self.playerVars)

和以下方法来强制解决问题:

and the follow method to force the resolution:

self.playerView.loadVideo(byId: "Rk6_hdRtJOE", startSeconds: 0.0, suggestedQuality: YTPlaybackQuality.HD720)


关于iPad:

爱德华(Edward)在评论中表示,iPad上存在一个小问题,这是因为这些设备似乎不适用于scalesPageToFit.目的是检查设备是否为iPad,然后缩放(缩小)scrollView以容纳小视角范围. 我已经在iPad air上进行了测试,并且可以正常工作.让我知道.

As reported by Edward in comments there was a little problem on iPads, that's because these devices seems don't apply scalesPageToFit. The goal is to check if the device is an iPad, then to scale (zooming out) the scrollView to host the little view bounds. I've tested on my iPad air and it works. Let me know.

这篇关于iOS YTPlayerView强制视频质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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