问题
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:
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.
来源:https://stackoverflow.com/questions/37290108/how-to-play-youtube-video-in-uiwebview-muted