WKWebView -requestMediaPlaybackStateWithCompletionHandler: Reporting Incorrect Playback State?

So I have a WKWebView with a loaded page. This page is playing an embedded Youtube video. The playback state is WKMediaPlaybackStatePlaying. All good.

Then I click a link on the page (navigation jumps to a new page) and the video is no longer playing. No video or audio. The navigationDelegate calls -webView:didFinishNavigation:

And my navigationDelegate call -requestMediaPlaybackStateWithCompletionHandler: on the webview from within -webView:didFinishNavigation:

And in the completion handler of -requestMediaPlaybackStateWithCompletionHandler: the playback state is WKMediaPlaybackStatePlaying but nothing is playing because we are on a new page....

I even tried calling -requestMediaPlaybackStateWithCompletionHandler: after a delay but that doesn't seem to make a difference (even gave it a delay as long as 5 seconds)

Breaking this down to a small sample project I get some strange results. Here's another example:

The web view's configuration is set to: mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;

  1. I set the navigation delegate.
  2. Then I load a page like macrumors.com/2023/10/09/video-of-prototype-touchscreen-imac-g3/
  3. After the page loads the -webView:didFinishNavigation: method is called. It is implemented like this:
#pragma mark - Navigation Delegate
-(void)webView:(WKWebView*)webView didFinishNavigation:(null_unspecified WKNavigation*)navigation
{
    NSLog(@"-webView:didFinishNavigation:");
    [self.webView requestMediaPlaybackStateWithCompletionHandler:^(WKMediaPlaybackState playbackState)
     {
        NSString *playbackStateString = webKitPlaybackStateToString(playbackState);
        NSLog(@"Playback State: %@",playbackStateString);
    }];
}

And WKMediaPlaybackStatePlaying logs out. There is a YouTube video on the page but it is not playing. I'm not sure if this "playback state" is referring to an animated ad or something but from what I can tell the page doesn't have any video or audio playback. Am I misunderstanding the meaning of WKMediaPlaybackState?

I filed FB13265380

Guess I'm going to have to try to do it with Javascript which is going to be difficult because media is often placed in iframes and communicating with user scripts across frame boundaries properly is kind of difficult.

Also is there a way I can execute a script (from the native app) on all frames (without knowing of the existence of iframes)? Ideally something like -evaluateJavaScriptInAllFrames:inContentWorld:perFrameResponseHander:completionHandler:

I can install a user script on all frames but when the native app wants to manipulate something in a subframe (say from a UIButton press), it doesn't seem like I can get ahold of a WKFrameInfo describing the iframe outside of the WKScriptMessageHandler callback.

or...Is there a place in AVFoundation where I can pick up WKWebview playback events to avoid using javascript?

Attempting to install a WKUserScript to do this has its problems. It seems if the loaded web page has uncaught javascript exceptions it can cause my WKUserScript to stop working.

The page (from the user's perspective) works fine but my user script can no longer send the native app messages. Apparently the web is the Wild West because there's a shocking number of websites that have errors on them so using javascript to monkey patch this functionality into WKWebView isn't going to work.

WKWebView -requestMediaPlaybackStateWithCompletionHandler: Reporting Incorrect Playback State?
 
 
Q