了解 YouTube 视频何时开始从 UIWebView 中播放

2023-12-30

我制作了一个播放 YouTube 视频的课程(感谢本网站好心人的帮助)。

现在我想知道如何知道 YouTube 视频何时开始播放。我想知道这一点,以便我可以在被告知要播放的视频和实际播放的视频之间显示“请稍候,正在加载...”指示器。

我使用开始播放视频stringByEvaluatingJavaScriptFromString:,当然,在我触发 javascript 命令来播放视频和视频实际播放之间存在轻微的延迟。

我熟悉 YouTube 视频和 javascript,所以我一直在看这个:[https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html] 因为我想知道 UIWebView 内的 javascript 事件侦听器是否可以检测到 YouTube 视频何时开始播放(侦听onStateChange事件)并触发 Objective C 方法。

这可能吗? UIWebViews 运行 javascript 的方式与 Web 浏览器相同吗?

有没有更简单/更好的方法来做到这一点?

这是我的 YouTube 课程:

// this is the only property declared in the .h file:
@property (strong, nonatomic) UIView * view

// the rest of this is the .m file:
#import "MyYouTube.h"
@interface MyYouTube()
@property (strong, nonatomic) NSDictionary * contentData;
@property (strong, nonatomic) UIWebView * webView;
@property (nonatomic) int videoOffset;
@end

@implementation MyYouTube
@synthesize view,contentData,webView,videoOffset;

- (MyYouTube*) initIntoView: (UIView*) passedView withContent: (NSDictionary*) contentDict {
    NSLog(@"YOUTUBE: begin init");
    self=[super init];
    videoOffset=0;
    view=[[UIView alloc] initWithFrame:passedView.bounds];
    [view setBackgroundColor:[UIColor blackColor]];
    [view setAutoresizesSubviews:YES];
    [view setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
    contentData=contentDict;

    NSString * compiledUrl=[[NSString alloc] initWithFormat:@"http://_xxx_.com/app/youtube.php?yt=%@",[contentData objectForKey:@"cnloc"]];
    NSURL * url=[[NSURL alloc] initWithString:compiledUrl];
    NSURLRequest * request=[[NSURLRequest alloc] initWithURL:url];

    webView=[[UIWebView alloc] initWithFrame:passedView.bounds];
    [webView loadRequest:request];
    [webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    [[webView scrollView] setScrollEnabled:NO];
    [[webView scrollView] setBounces:NO];
    [webView setMediaPlaybackRequiresUserAction:NO];
    [webView setDelegate:self];

    NSLog(@"YOUTUBE: self: %@",self);
    NSLog(@"YOUTUBE: delegate: %@",webView.delegate);

    [view addSubview:webView];
    NSLog(@"YOUTUBE: end init");
    return self;
}

-(void)webViewDidFinishLoad:(UIWebView*)myWebView {
    NSLog(@"YOUTUBE: send play command");
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"playVideo(%d)", videoOffset]];
}

@end

非常感谢。


我相信这可以通过新的iOS7工作JavaScript核心框架 http://blog.bignerdranch.com/3784-javascriptcore-and-ios-7/(虽然我没有亲自测试过)。

我已经测试过并且似乎工作正常的是这种方法:

您可以拥有一个计时器,而不是在不同的事件更改上注册侦听器queries玩家每秒的状态。

首先,您可以在玩家类上定义一些状态(对应于YouTube 的状态 https://developers.google.com/youtube/js_api_reference#Playback_status)(在头文件中):

typedef NS_ENUM(NSInteger, YoutubeVideoState){
    YOUTUBE_VIDEO_STATE_UNSTARTED   = -1,
    YOUTUBE_VIDEO_STATE_ENDED       = 0,
    YOUTUBE_VIDEO_STATE_PLAYING     = 1,
    YOUTUBE_VIDEO_STATE_PAUSED      = 2,
    YOUTUBE_VIDEO_STATE_BUFFERING   = 3,
    YOUTUBE_VIDEO_STATE_VIDEO_CUED  = 5
};

那么你可以有一个readonly属性来获取状态(也在头文件中):

@property (nonatomic, readonly) YoutubeVideoState state;

其实施方式如下:

- (YoutubeVideoState)state
{
    return (YoutubeVideoState)[[self.webView stringByEvaluatingJavaScriptFromString:@"getPlayerState()"] integerValue];
}

现在,您可以从实例中请求获取每个计时器调用中的当前状态,例如:

YoutubeVideoState currentState = myPlayerInstance.state;

并可能通过以下方式决定您的下一步行动switch or an if陈述:

if (currentState == YOUTUBE_VIDEO_STATE_PLAYING) {
    // Start your engines!
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

了解 YouTube 视频何时开始从 UIWebView 中播放 的相关文章

随机推荐