2016-05-18 57 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

相關問題