【发布时间】:2013-12-13 09:47:35
【问题描述】:
我制作了一个播放 YouTube 视频的课程(感谢本网站好心人的帮助)。
现在我想知道如何知道 YouTube 视频何时开始播放。我想知道这一点,以便在被告知播放的视频和实际播放的视频之间显示“请稍候,正在加载...”指示符。
我使用 stringByEvaluatingJavaScriptFromString: 开始播放视频,当然,在我启动 javascript 命令以播放视频和实际播放视频之间会有一点延迟。
我熟悉 YouTube 视频和 javascript,所以我一直在看这个:[https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html] 因为我想知道 UIWebView 中的 javascript 事件监听器是否可以检测到 YouTube 视频何时开始播放(监听 @987654323 @event) 并触发一个 Objective C 方法。
这可能吗? UIWebViews 是否以与 Web 浏览器相同的方式运行 javascript?
有更简单/更好的方法吗?
这是我的 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
非常感谢。
【问题讨论】:
标签: javascript ios objective-c uiwebview youtube