YouTube Autoplay Possibilities In IOS

一世执手 提交于 2019-12-04 15:07:47

I found the answer: If you put

webView.mediaPlaybackRequiresUserAction=FALSE; In the webViewDidFinishLoad: then it will autoplay and still let you use the other buttons as well. This worked like a charm and suggest using it.

Dannyy

To check if the playback is finished I used the following method:

First, set the delegate of your webview to self and implement this:

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    UIButton *b = [self findButtonInView:_webView];
    [b sendActionsForControlEvents:UIControlEventTouchUpInside]; //autoplay :)
    [[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject] setTag:1234];  //tag it 
}

then start a timer:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];

and check:

BOOL playing;
- (void)onTimer:(NSTimer *)t {
    id _topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
    if ([_topView tag]==1234)
    {
         playing=YES;
    }
    else {
        if (playing==YES) { //was playing, but now not anymore
            //dismiss your view, or do other things.
            [t invalidate];
            playing=NO;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!