How can I unmute (and turn off autoplay) a YouTube video for twentyseventeen?

天大地大妈咪最大 提交于 2019-12-23 10:56:18

问题


I have a lecture on YouTube that I would like to use as the header movie for my twentyseventeen child themed website. I would like it with sound and without autoplay. (As viewed on Youtube, the video has sound intact.)

Following [this question about autoplay], I seta a video URL of https://www.youtube.com/watch?v=1dYAYBNU6qM&autoplay=0. The behavior does not appear to have changed. It starts immediately, but sound is muted.

How, with twentyseventeen, do I have an option of a media file that starts paused, and begins to play, with sound, if the user hits the 'Play' button?

Thanks,


回答1:


You can use global object wp for get access to youtube player functions. Youtube video in twentyseventeen theme loads wp-custom-header.js file and creating new object in 384 line.

Here is some solution you can use:

var ww_timer = setTimeout(function ww_video() {
    if(wp.customHeader.handlers.youtube.player == null){
        ww_timer = setTimeout(ww_video, 50);
    }else {
        if(typeof wp.customHeader.handlers.youtube.player.unMute === "function") {
            wp.customHeader.handlers.youtube.player.unMute();
            wp.customHeader.handlers.youtube.player.stopVideo();
        }else{
            ww_timer = setTimeout(ww_video, 50);
        }
    }
}, 50);

This code goes to my_js.js file( I created it in the main directory of active child theme. You can add this code to another .js, if you have it ) of your active child theme. Also, we need to update functions.php file using this code:

function ww_youtube_functions(){
  wp_enqueue_script('ww_youtube_video',get_stylesheet_directory_uri().'/my_js.js',array('wp-custom-header'),false, true);
}
add_action('wp_enqueue_scripts', 'ww_youtube_functions');

Required part of this code is array('wp-custom-header'): enqueue script with dependence with script wp-custom-header.

setTimeout is not best way. I believe, that it can be done with more elegant code.

But its tested and working.



来源:https://stackoverflow.com/questions/48362721/how-can-i-unmute-and-turn-off-autoplay-a-youtube-video-for-twentyseventeen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!