OGeek|极客世界-中国程序员成长平台

标题: ios - 如何使用 wkwebview 在我的应用中自动播放视频 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:05
标题: ios - 如何使用 wkwebview 在我的应用中自动播放视频

我正在使用 wkwebview 来展示我的网站。我的网站有一个视频。当网站显示时,视频无法自动播放。请问有什么方法可以让视频自动播放吗?



Best Answer-推荐答案


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

关于ios - 如何使用 wkwebview 在我的应用中自动播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35264305/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4