ios - 在 UIWebview,iOS 中自动播放视频

标签 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.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];

选项 3

How to autoplay a YouTube video in a UIWebView

I dont want auto play the in UIWebview play youtube video

但所有选项都没有帮助我,

最佳答案

谢谢@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]; 

关于ios - 在 UIWebview,iOS 中自动播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34429042/

相关文章:

android - 如何在 Flutter_webview 中显示或下载 pdf 文件?

ios - 如何重新显示 IsLoaded 的 ViewController

iphone - iOS 在线程中获取 UITextField 文本,显示 '_WebThreadLockFromAnyThread'

ios - 如何再次提示 CKApplicationPermissionUserDiscoverability

ios - 如何 "virtually"点击导航栏中的后退按钮

ios - 使用 Xcode 5 和 iOS 6.1 作为基础 SDK 问题

ios - Swift:XLPagerTabStrip:ButtonBarPagerTabStripViewController:未设置 ChildViewController IBOutlets

objective-c - 包标识符应该是小写字母还是驼峰字母?

ios - Swift:NSJSONSerialization.JSONObjectWithData() 失败,JSON utf-8

ios - 多层次深度泛型/协议(protocol)的 Swift 泛型和协议(protocol)问题