Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
961 views
in Technique[技术] by (71.8m points)

ios - How to play youtube video in UIWebView muted?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...