YouTube Autoplay Possibilities In IOS

2019-04-14 18:14发布

问题:

I have created a webView:

videoView.delegate = self;
videoView.userInteractionEnabled = true;
[videoView loadRequest:[[NSURLRequest alloc]initWithURL:[[NSURL alloc]   initWithString:@"website"]]];

I have html/javascript to handle the video's loading as well as the autoplay and next video. This all works, but my problem is that IOS autoloads the quicktime - like player, which is also a good thing, but I do not know how to control that, nor do I know how to get the video to play once that has happened. I would like the button that shows up in the player to autoplay. I would like that to happen anytime a new video is loaded.

JS code works like this. I take in a list and that is my playlist. I do not load a playlist from youTube I just make my own list and when end of video is called I load a new video by id. I also autoplay the video which is what gives me the button that IOS supplies anytime a video loads. Also, I am using an iframe to do all of this, with the youTube api's that is how I have control on the js side of things.

Question: Is there a way to control the play via code? If so how, also when the player is already loaded and I load another video how can I get that to autoplay as well?

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.

回答1:

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.



回答2:

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;
        }
    }
}