iOS Question iOS 13 YouTube Player

ziomorgan

Active Member
Licensed User
Longtime User
Hi,
i've updated my iPhone to iOS 13.3 and now the AutoPlay for YouTube video doesn't work (B4i 6.80).

My log is:

Method not found: setMediaPlaybackRequiresUserAction:, target: <WKWebView: 0x102184600; frame = (0 0; 375 667); animations = { bounds.origin=<CASpringAnimation: 0x28205a560>; bounds.size=<CASpringAnimation: 0x28205a6a0>; position=<CASpringAnimation: 0x2820587c0>; }; layer = <CALayer: 0x2820697e0>>

How can I correct the error?
 

Semen Matusovskiy

Well-Known Member
Licensed User
'WebView' in old releases was a wrapper for UIWebView. Now 'WebView' is a wrapper for WKWebView, because Apple Store does not accept UIWebView any more.

setMediaPlaybackRequiresUserAction was in UIWebView. In WKWebView you need to create WKWebViewConfiguration, for example
B4X:
Dim noWC As NativeObject
noWC = noWC.Initialize ("WKWebViewConfiguration").RunMethod ("alloc", Null).RunMethod ("init", Null)
and to set mediaTypesRequiringUserActionForPlayback
B4X:
noWC.SetField ("mediaTypesRequiringUserActionForPlayback", ...)
Possible values - see https://developer.apple.com/documen...ediatypesrequiringuseractionfor?language=objc

About autoplay. HTML5 exactly works, but about YouTube I am not so sure.
 
Upvote 0

ziomorgan

Active Member
Licensed User
Longtime User
This is the code that worked without problems for the previous version of ios. Your suggestions don't work for autoplay YouTube video. Any more suggestions?

B4X:
    Private no As NativeObject = wvYouTubePlayer
    no.RunMethod("setMediaPlaybackRequiresUserAction:", Array(False))

   'Fly to YouTube website
    wvYouTubePlayer.LoadHtml( _
            $"<!DOCTYPE html>
              <html>
                  <head>
                      <style>body{margin:0px 0px 0px 0px;}</style>
                  </head>
                  <body>
                      <div id="player"></div>
                      <script> var tag = document.createElement('script');
                               tag.src = "https://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:'${pg.RootPanel.Width}',
                                           height:'${pg.RootPanel.Height}',
                                           videoId:'${sIDVideo}',
                                           playerVars: { 'rel' : 0 },
                                           events:
                                           {
                                                'onReady': onPlayerReady,
                                           }
                                       });
                                   }
                               function onPlayerReady(event)
                                   {
                                       event.target.playVideo();
                                   }
                      </script>
                  </body>
              </html>"$)
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User

Autoplay and Scripted Playback
Warning: To prevent unsolicited downloads over cellular networks at the user’s expense, embedded media cannot be played automatically in Safari on iOS — the user always initiates playback.

Due to this restriction, functions and parameters such as autoplay, playVideo(), loadVideoById() won't work in all mobile environments.
 
Upvote 0
Top