了解YouTubevideo何时从UIWebView中开始播放
我制作了一个播放YouTubevideo的课程(感谢本网站的善意帮助)。
现在我想知道我是如何知道YouTubevideo何时开始播放的。 我想知道这一点,所以我可以在播放的video和实际播放的video之间显示“请等待,正在加载…”指示。
我使用stringByEvaluatingJavaScriptFromString:
开始播放video,当然我在启动javascript命令以播放video和video实际播放之间有一点延迟。
我熟悉YouTubevideo和javascript,所以我一直在看这个:[ https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/ObjCFromJavaScript.html ]因为我想知道是否UIWebView中的javascript事件侦听器可以检测YouTubevideo何时开始播放(侦听onStateChange
事件)并触发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
非常感谢。
我相信这可以通过新的JavaScriptCore框架在iOS7上运行(虽然我没有亲自测试过它)。
虽然我测试了它似乎工作正常这种方法:
您可以使用定时器每秒查询播放器的状态,而不是在不同的事件更改上注册侦听器。
首先,您可以在播放器类上定义一些状态(对应于Youtube的状态 )(在头文件中):
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
或if
语句决定你的下一个动作:
if (currentState == YOUTUBE_VIDEO_STATE_PLAYING) { // Start your engines! }