【问题标题】:How do I make UIWebView pause/play video content programmatically如何以编程方式使 UIWebView 暂停/播放视频内容
【发布时间】:2015-10-03 08:17:12
【问题描述】:

我正在让我的应用通过 UIWebView 加载 youtube 视频,但需要能够让它在特定时间暂停和播放。我已经查看了这里的所有答案,但没有一个有效。我试过这个:

webView.stringByEvaluatingJavaScriptFromString("var videos = document.querySelectorAll(\"video\"); for (var i = videos.length - 1; i >= 0; i--) { videos[i].pause(); };")

但这无济于事。如果重要,我将通过以下方式加载视频:

webView.allowsInlineMediaPlayback = true
webView.mediaPlaybackRequiresUserAction = false    

let embededHTML = "<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='0' height='0' src='http://www.youtube.com/embed/\(videoID)?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></body></html>"

webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().resourceURL)

【问题讨论】:

  • 您使用的是哪个 iOS 版本?你已经看过this 线程了吗?或者,您可以尝试this 答案中提出的修复(如果您想使用pauseVideo(),我假设遵循类似的逻辑)。
  • 第一个链接很完美!谢谢!

标签: ios swift iframe uiwebview youtube-api


【解决方案1】:

由于not_a_bot 链接,使用ytplayer.pauseVideo()ytplayer.playVideo() 完美。

webView.stringByEvaluatingJavaScriptFromString("ytplayer.pauseVideo()")

webView.stringByEvaluatingJavaScriptFromString("ytplayer.playVideo()")

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2022-08-11
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多