在iOS的UIWebview中自动播放视频
我有URL,例如在iOS的UIWebview中自动播放视频,ios,objective-c,swift,Ios,Objective C,Swift,我有URL,例如https://www.youtube.com/watch?v=c2ccXwwmcnA。如果我将此URL传递给UIWebView它会自动播放视频。我试过以下方法 选项1 @property (weak, nonatomic) IBOutlet UIWebView *webvie; NSString *url=@"https://www.youtube.com/watch?v=7jYa7dfrXKU"; self.webvie.allowsInlineMediaPl
https://www.youtube.com/watch?v=c2ccXwwmcnA
。如果我将此URL传递给UIWebView
它会自动播放视频。我试过以下方法
选项1
@property (weak, nonatomic) IBOutlet UIWebView *webvie;
NSString *url=@"https://www.youtube.com/watch?v=7jYa7dfrXKU";
self.webvie.allowsInlineMediaPlayback = YES;
self.webvie.scrollView.bounces = NO;
self.webvie.mediaPlaybackRequiresUserAction = NO;
[self.webvie loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
选项2
NSString *youTubeHTMLTemplate = @"<html><head><style type=\"text/css\">body { background-color: transparent;color: white;}</style></head><body style=\"margin:0\"><embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" width=\"%0.0f\" height=\"%0.0f\"></embed></body></html>";
finalHtml = [NSString stringWithFormat:youTubeHTMLTemplate, fullYopuTubeUrl, htmlFrameWidth, htmlFrameHeight];
NSString*youTubeHTMLTemplate=@“主体{背景色:透明;颜色:白色;}”;
finalHtml=[NSString stringWithFormat:youTubeHTMLTemplate,fullYopuTubeUrl,htmlFrameWidth,htmlFrameHeight];
选项3
但是所有的选项对我都没有帮助,谢谢@Kyle,我尝试了你的评论,我在URL后添加了&autoplay=1
,效果非常好。答案是
NSString *url=@"https://www.youtube.com/watch?v=7jYa7dfrXKU&autoplay=1";
[self.webvie loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
self.webvie.scrollView.bounces = NO;
[self.webvie setMediaPlaybackRequiresUserAction:NO];
我认为您必须在embed
标记中添加autoplay
属性。您想自动播放视频还是不想自动播放视频?还有,你有没有考虑过使用?我们已经尝试过了,我们附加了播放视频的ID链接,但我的要求是先加载URL,然后我需要视频自动播放。@KyleEmmanuel——等等,我试试看once@ShangariC:我只是想知道当苹果因为占用用户数据而禁止自动播放时,你怎么能在UIWebView中自动播放?