如何使用wkwebview在我的应用程序中自动播放视频

时间:2016-02-08 07:26:06

标签: ios video ios8 autoplay

我正在使用wkwebview来显示我的网站。我的网站有一个视频。当网站显示时,视频无法自动播放。这是让视频自动播放的任何方法吗?

2 个答案:

答案 0 :(得分:1)

以下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 player_parameters

答案 1 :(得分:0)

仅对ios 9.0,10.0使用mediaTypesRequiringUserActionForPlayback 指涉: @property(非原子)BOOL需要UserActionForMediaPlayback API_DEPRECATED_WITH_REPLACEMENT(&#34; mediaTypesRequiringUserActionForPlayback&#34;,ios(9.0,10.0));