0

我有一个 youtube 视频正在播放,现在单击 UIButton 进行静音我需要将视频静音。为此,我有 javascript 代码,但我需要在 UIButton 单击时调用它。我怎么做?

    -(void)viewWillAppear:(BOOL)animated
{
    float width = 309.0f;
    float height = 196.0f;
    UIWebView *wv = [[UIWebView alloc] init];
    wv.frame = CGRectMake(0, 0, width, height);

    _webViewVideo.allowsInlineMediaPlayback = YES;
    _webViewVideo.mediaPlaybackRequiresUserAction = NO;

//    @"<!DOCTYPE html><html> <body><div id=\"player\"></div><script>var tag = document.createElement('script');tag.src = \"https://www.youtube.com/iframe_api\";var firstScriptTag = document.getElementsByTagName('script')[0];firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);var player;function onYouTubeIframeAPIReady() {player = new YT.Player('player', {height: '390',width: '640',videoId: 'M7lc1UVf-VE', autoplay:1, controls:0, events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}function onPlayerReady(event) {event.target.playVideo();}var done = false;function onPlayerStateChange(event) {if (event.data == YT.PlayerState.PLAYING && !done) {setTimeout(stopVideo, 6000);done = true;}}function stopVideo() {player.stopVideo();}</script></body></html>";

    NSString *htmlString =@"<!DOCTYPE html><html> <body><div id=\"player\"></div><script>var tag = document.createElement('script');tag.src = \"https://www.youtube.com/iframe_api\";var firstScriptTag = document.getElementsByTagName('script')[0];firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);var player;function onYouTubeIframeAPIReady() {player = new YT.Player('player', {height: '196',width: '309',videoId: 'GOiIxqcbzyM',playerVars: {playsinline: 1, controls: 0}, events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});} function **muteVideo**(){player.mute();}function onPlayerReady(event) {event.target.playVideo();}var done = false;function onPlayerStateChange(event) {if (event.data == YT.PlayerState.PLAYING && !done) {muteVideo();done = true;}}function stopVideo() {}</script></body></html>";
    _webViewVideo.delegate = self;

    [_webViewVideo loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] resourceURL]];
}
- (IBAction)btnMute_click:(id)sender {

    [_webViewVideo stringByEvaluatingJavaScriptFromString:@"muteVideo();"]; //not working
}

我如何在这里调用 muteVideo?

4

1 回答 1

0

我试过这个,它似乎正在工作。如果它是正确的,给它一个镜头和投票。:-)

[_webViewVideo loadHTMLString:@"muteVideo();" baseURL:[[NSBundle mainBundle] resourceURL]];
于 2014-02-25T03:15:08.847 回答