2016-05-18 98 views
1

我正在使用UIWebView播放嵌入的YouTube视频,在iOS 9上为iPhone。如何播放UIWebView中的YouTube视频静音?

然而,由于的已知问题,音量控制不工作,即调用player.mute()或player.setVolume(0)不会在所有的工作: https://github.com/youtube/youtube-ios-player-helper/issues/20

我想知道是否有人成功解决了这个问题?你能分享一下你的方法吗?

我使用的样品嵌入HTML:

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

谢谢!

+0

解决办法可能是监听WebView的私人通知,并从通知对象获取播放器对象,然后将其静音。 –

+0

@MuhammadZeeshan你有什么例子吗? – RainCast

回答

3

首先你不能使用javascript检查设置音量this doc并阅读Volume Control in JavaScript部分。 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]; 
} 

这似乎是工作在模拟器的罚款。但是这种方法正在使用一些私有属性。使用需要您自担风险;)并且不要忘记删除观察者。

+0

它的工作,感谢穆罕默德。你知道这是否合法吗?我看到其他职位说这是“哈克”。 http://stackoverflow.com/questions/26154823/how-to-get-the-url-of-currently-playing-video-in-uiwebview – RainCast

+0

这是hacky,因为它的webview的内部通知。最近我开发了一个应用程序,正在收听此通知,应用商店审核团队批准了该应用程序。 –

+0

明白了,谢谢你的解释。但是, – RainCast

相关问题