简体   繁体   English

当 WKMediaPlaybackState 改变时如何得到通知

[英]how to get notified when WKMediaPlaybackState changes

How can add an observer that runs a callback when the value of the webView's WKMediaPlaybackState changes当 webView 的 WKMediaPlaybackState 的值发生变化时,如何添加一个运行回调的观察者

I'm trying to do some changes when the vimeo video in the webView is paused当 webView 中的 vimeo 视频暂停时,我正在尝试进行一些更改

this is my code这是我的代码

@IBOutlet weak var vimeoVideoPlayer: WKWebView!

func buildVimeoVideoPlayer(with video: Video) {
    let url = URL(string: "https://player.vimeo.com/video/\(video.id)")!
    let requestObj = URLRequest(url: url)
    vimeoVideoPlayer.load(requestObj)
    vimeoVideoPlayer.contentMode = UIView.ContentMode.scaleAspectFit
    vimeoVideoPlayer.configuration.allowsAirPlayForMediaPlayback = true
    vimeoVideoPlayer.configuration.allowsInlineMediaPlayback = true
    vimeoVideoPlayer.configuration.allowsPictureInPictureMediaPlayback = true
    
    vimeoVideoPlayer.navigationDelegate = self
    vimeoVideoPlayer.scrollView.isScrollEnabled = false

    vimeoVideoPlayer.requestMediaPlaybackState { mediaState in
            switch mediaState {
            case .paused:
                print("the video was Paused")
            case .playing:
                print("the video is playing")
            case .suspended:
                print("the video is suspended")
            default:
                break
            }
    }
}

at the moment I'm using the requestMediaPlaybackState function but this only runs once目前我正在使用requestMediaPlaybackState function 但这只运行一次

You can observe the video player's state directly from java script and to do that you should add needed event listeners to the video object and then post a state to native via webkit.messageHandlers : You can observe the video player's state directly from java script and to do that you should add needed event listeners to the video object and then post a state to native via webkit.messageHandlers :

webView.configuration.userContentController.add(self, name: "playbackMessageHandler")

let js = """
    function onPlaybackState(playbackState) {
        const { webkit: { messageHandlers: { playbackMessageHandler } } } = window;
        playbackMessageHandler?.postMessage({ playbackState });
    };

    window.addEventListener('load', (event) => {
        const video = document.querySelector('video');
        video?.addEventListener('play', (event) => onPlaybackState('playing'));
        video?.addEventListener('pause', (event) => onPlaybackState('paused'));
        video?.addEventListener('suspend', (event) => onPlaybackState('suspended'));
    });
"""

let script = WKUserScript(source: js, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
webView.configuration.userContentController.addUserScript(script)

webView.load(...)

...

extension ViewController: WKScriptMessageHandler {
    
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        guard let body = message.body as? [String : String], let playbackState = body["playbackState"] else { return }
        print(playbackState)
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM