2
votes

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!

1
work around could be to listen WebView's private notification and get the player object from notification object and then mute it.Muhammad Zeeshan
@MuhammadZeeshan do you have any examples?RainCast

1 Answers

4
votes

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.