我如何使用 wkwebview 在我的应用程序中自动播放视频
how can i auto play a video in my app using wkwebview
我正在使用 wkwebview
来显示我的网站。我的网站有一个视频。当网站显示时,视频不能autoplay.is有什么方法可以让视频自动播放?
以下 function
为我提供支持 auto play
- (void)loadwebViewToPlay {
NSString * videoHtml = @"<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'200', height:'200', videoId:'bHQqvYy5KYo', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
UIWebView * webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
webview.backgroundColor = [UIColor clearColor];
webview.opaque = NO;
//videoView.delegate = self;
[self.view addSubview:webview];
webview.mediaPlaybackRequiresUserAction = NO;
[webview loadHTMLString:videoHtml baseURL:[[NSBundle mainBundle] resourceURL]];
}
更多帮助可以参考iframe_api_reference OR player_parameters
仅对 ios 9.0、10.0 使用 mediaTypesRequiringUserActionForPlayback
指代:
@属性(非原子)BOOL requiresUserActionForMediaPlayback API_DEPRECATED_WITH_REPLACEMENT("mediaTypesRequiringUserActionForPlayback", ios(9.0, 10.0));
我正在使用 wkwebview
来显示我的网站。我的网站有一个视频。当网站显示时,视频不能autoplay.is有什么方法可以让视频自动播放?
以下 function
为我提供支持 auto play
- (void)loadwebViewToPlay {
NSString * videoHtml = @"<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'200', height:'200', videoId:'bHQqvYy5KYo', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
UIWebView * webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
webview.backgroundColor = [UIColor clearColor];
webview.opaque = NO;
//videoView.delegate = self;
[self.view addSubview:webview];
webview.mediaPlaybackRequiresUserAction = NO;
[webview loadHTMLString:videoHtml baseURL:[[NSBundle mainBundle] resourceURL]];
}
更多帮助可以参考iframe_api_reference OR player_parameters
仅对 ios 9.0、10.0 使用 mediaTypesRequiringUserActionForPlayback 指代: @属性(非原子)BOOL requiresUserActionForMediaPlayback API_DEPRECATED_WITH_REPLACEMENT("mediaTypesRequiringUserActionForPlayback", ios(9.0, 10.0));