2

I am using UIWebView to play an embedded youtube video, on iOS 9 for iphone.

However, due to a known issue, the volume control is not working, i.e. calling player.mute() or player.setVolume(0) doesn't work at all: https://github.com/youtube/youtube-ios-player-helper/issues/20

I am wondering if anyone has successfully worked around this? Could you share your method.

The sample embedded html I am using:

    <html><body style='margin:0px;padding:0px;'>  
    <script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>
    var player;
    function onYouTubeIframeAPIReady()
    {player=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}
    function onPlayerReady(event){player.mute();player.setVolume(0);player.playVideo();}
    </script>
    <iframe id='playerId' type='text/html' width='1280' height='720' 
src='https://www.youtube.com/embed/R52bof3tvZs?enablejsapi=1&rel=0&playsinline=1&autoplay=1&showinfo=0&autohide=1&controls=0&modestbranding=1' frameborder='0'>
    </body></html>

Thanks!

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
RainCast
  • 4,134
  • 7
  • 33
  • 47

1 Answers1

4

First of all you cannot set the volume using javascript check this doc and read Volume Control in JavaScript section. Found here.

Secondly i tried this:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemBecameCurrentNotif:)
                                             name:@"AVPlayerItemBecameCurrentNotification"
                                           object:nil];


- (void)playerItemBecameCurrentNotif:(NSNotification*)notification {
    AVPlayerItem *playerItem = notif.object;
    AVPlayer *player = [playerItem valueForKey:@"player"];
    [player setVolume:0.0];
}

This seems to be working fine in simulator. However this method is using some private properties. Use at your own risk ;) and don't forget to remove observer.

Community
  • 1
  • 1
Muhammad Zeeshan
  • 2,441
  • 22
  • 33
  • It works, thanks Muhammad. Do you know if this is a legit approach? I've seen other post saying that this is "hacky". http://stackoverflow.com/questions/26154823/how-to-get-the-url-of-currently-playing-video-in-uiwebview – RainCast May 19 '16 at 21:20
  • This is hacky because its a webview's internal notification. Recently i developed an app that is listening to this notification and app store review team approved it. – Muhammad Zeeshan May 20 '16 at 04:25
  • 3
    any solution in WKWebView how can do that ? – Virani Vivek Apr 01 '19 at 11:49