我上了一堂课,播放YouTube视频(感谢这个网站的友善帮助).
现在,我想知道如何确定YouTube视频何时开始播放.我想知道这一点,所以我可以在正在播放的视频与实际播放的视频之间显示“请稍候,正在加载…”指示.
我使用stringByEvaluatingJavaScriptFromString:开始播放视频,当然,在触发javascript命令以开始播放视频和实际播放视频之间,会有一点延迟.
我熟悉YouTube视频和javascript,因此我一直在研究:[https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html],因为我想知道UIWebView中的JavaScript事件侦听器是否可以检测YouTube视频何时开始播放(监听onStateChange事件)并触发目标C法.
这可能吗? UIWebViews是否以与网络浏览器相同的方式运行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
非常感谢.
解决方法:
我相信这可以通过新的JavaScriptCore framework在iOS7上运行(尽管我还没有亲自测试过).
虽然我已经测试过,但似乎可以正常工作的是这种方法:
您可以使用一个计时器每秒查询播放器的状态,而不是在不同的事件更改上注册侦听器.
首先,您可以在播放器类上定义一些状态(对应于Youtube’s states)(在头文件中):
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
};
然后,您可以拥有一个只读属性来获取状态(也在头文件中):
@property (nonatomic, readonly) YoutubeVideoState state;
可以像这样实现:
- (YoutubeVideoState)state
{
return (YoutubeVideoState)[[self.webView stringByEvaluatingJavaScriptFromString:@"getPlayerState()"] integerValue];
}
现在,您可以从实例中要求在每次计时器调用中获取当前状态,例如:
YoutubeVideoState currentState = myPlayerInstance.state;
并可能通过switch或if语句决定下一步的操作:
if (currentState == YOUTUBE_VIDEO_STATE_PLAYING) {
// Start your engines!
}