如何在UIWebView中播放youtube视频静音?

问题描述:

我在iOS 9 for iphone上使用UIWebView播放嵌入式YouTube视频。

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

但是,由于已知问题,音量控制不起作用,即调用player.mute()或者player.setVolume(0)根本不起作用:
https://github.com/youtube/youtube-ios-player-helper/issues/20

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.

我使用的示例嵌入式html:

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>

谢谢!

首先,您无法使用javascript检查设置音量此文档并阅读 JavaScript中的音量控制部分。 在此处找到

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

其次我试过这个:

[[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.