【问题标题】:Knowing when a YouTube video starts playing from within a UIWebView了解 YouTube 视频何时从 UIWebView 开始播放
【发布时间】: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


    【解决方案1】:

    我相信这可以通过新的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
    };
    

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

    @property (nonatomic, readonly) YoutubeVideoState state;
    

    实现如下:

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

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

    YoutubeVideoState currentState = myPlayerInstance.state;
    

    并可能通过switchif 声明决定您的下一步行动:

    if (currentState == YOUTUBE_VIDEO_STATE_PLAYING) {
        // Start your engines!
    }
    

    【讨论】:

    猜你喜欢
    • 2014-10-04
    • 2012-03-19
    • 2017-02-16
    • 2011-05-05
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    相关资源
    最近更新 更多