2012-01-15 8 views
30
<iframe class="youtube-player" type="text/html" src="http://www.youtube.com/embed/JW5meKfy3fY?wmode=opaque&autohide=1&autoplay=1&volume=0&vol=0&mute=1" frameborder="0">&lt;br /&gt;</iframe> 

वीडियो म्यूट नहीं किया गया है! मैं जब वह पहली बार खेलता मात्रा 0 होना चाहता हूँ ...मैं स्वचालित रूप से एक यूट्यूब वीडियो (आईफ्रेम एपीआई) कैसे म्यूट कर सकता हूं?

उत्तर

60

यूट्यूब के माध्यम से muting प्रदान नहीं करते यूआरएल पैरामीटर (http://code.google.com/apis/youtube/player_parameters.html देखें)।

आपको इसके लिए जावास्क्रिप्ट का उपयोग करना होगा। विवरण के लिए http://code.google.com/apis/youtube/js_api_reference.html देखें।

हालांकि, पेज ऊपर लिंक पर चेतावनी कृपया ध्यान दें:। "यूट्यूब जावास्क्रिप्ट प्लेयर API की निंदा 27 जनवरी, 2015 को घोषणा की गई थी यूट्यूब फ्लैश एम्बेड करता है भी अमान्य हो जाने के बारे में अधिक जानकारी के लिए प्रतिवाद नीति देखें। कृपया अपने अनुप्रयोगों को IFrame API पर माइग्रेट करें, जो बुद्धिमानी से जो भी एम्बेडेड प्लेयर - HTML() या Flash() - क्लाइंट का समर्थन करता है, का उपयोग कर सकते हैं। "

एचटीएमएल

<iframe class="youtube-player" id="player" type="text/html" src="http://www.youtube.com/embed/JW5meKfy3fY?wmode=opaque&autohide=1&autoplay=1&enablejsapi=1" frameborder="0">&lt;br /&gt;</iframe> 

कृपया ध्यान दें enablejsapi = 1 यूआरएल में।

जावास्क्रिप्ट

var player = iframe.getElementById('player'); 
player.mute(); 

अद्यतन

पिछला कोड कुछ मुद्दों था और वर्तमान एपीआई (playerVars वाक्य रचना गलत था) के साथ काम नहीं किया। यहां अद्यतन कोड है। आपको आवश्यक पैरामीटर के साथ आपको टिंकर करने की आवश्यकता हो सकती है।

  
 
    <div id="player"></div> 
 
    <script> 
 
     // 1. This code loads the IFrame Player API code asynchronously. 
 
     var tag = document.createElement('script'); 
 

 
     tag.src = "https://www.youtube.com/iframe_api"; 
 
     var firstScriptTag = document.getElementsByTagName('script')[0]; 
 
     firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 
 

 
     // 2. This function creates an <iframe> (and YouTube player) 
 
     // after the API code downloads. 
 
     var player; 
 
     function onYouTubeIframeAPIReady() { 
 
     player = new YT.Player('player', { 
 
      height: '100%', 
 
      width: '100%', 
 
      playerVars: { 
 
        autoplay: 1, 
 
        loop: 1, 
 
        controls: 0, 
 
        showinfo: 0, 
 
        autohide: 1, 
 
        modestbranding: 1, 
 
        vq: 'hd1080'}, 
 
      videoId: '1pzWROvY7gY', 
 
      events: { 
 
      'onReady': onPlayerReady, 
 
      'onStateChange': onPlayerStateChange 
 
      } 
 
     }); 
 
     } 
 

 
     // 3. The API will call this function when the video player is ready. 
 
     function onPlayerReady(event) { 
 
     event.target.playVideo(); 
 
     player.mute(); 
 
     } 
 

 
     var done = false; 
 
     function onPlayerStateChange(event) { 
 
     
 
     } 
 
     function stopVideo() { 
 
     player.stopVideo(); 
 
     } 
 
    </script>

+3

यह कहता है "iframe है परिभाषित नहीं"? – TIMEX

+0

क्या आप मुझे बता सकते हैं कि क्या गलत है? http://jsfiddle.net/9RjzU/ धन्यवाद – TIMEX

+2

मैंने सही कोड के साथ पहेली को अद्यतन किया। http://jsfiddle.net/9RjzU/3/ –

3

आप वीडियो प्लेयर चयन करें और फिर अपने मात्रा निर्धारित कर सकते हैं:

var mp = iframe.getElementById('movie_player'); 
mp.setVolume(0); 

स्रोत: http://userscripts.org/scripts/review/49366

+1

धन्यवाद का प्रयास करें, लेकिन यह काम नहीं करता। http://jsfiddle.net/69uh2/ मैं क्या गलत कर रहा हूँ? – TIMEX

+0

म्यूट बेहतर है, इसके साथ आप मूल वॉल्यूम को बदले बिना अनम्यूट कर सकते हैं। –

+1

अपने बेवकूफ टाइमएक्स को ठीक करने की कोशिश कर रहा है और पूरे गीत को सुनने के लिए रुकना पड़ा है ... – Robbo

4

player_api जून 25 पर समाप्त हो जाएगी 2015 खेलने यूट्यूब वीडियो के लिए वहाँ एक नए API IFRAME_API

यह निम्न कोड की तरह लग रहा है:

<!-- 1. The <iframe> (and video player) will replace this <div> tag. --> 
<div id="player"></div> 

<script> 
    // 2. This code loads the IFrame Player API code asynchronously. 
    var tag = document.createElement('script'); 

    tag.src = "https://www.youtube.com/iframe_api"; 
    var firstScriptTag = document.getElementsByTagName('script')[0]; 
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 

    // 3. This function creates an <iframe> (and YouTube player) 
    // after the API code downloads. 
    var player; 
    function onYouTubeIframeAPIReady() { 
    player = new YT.Player('player', { 
     height: '390', 
     width: '640', 
     videoId: 'M7lc1UVf-VE', 
     events: { 
     'onReady': onPlayerReady, 
     'onStateChange': onPlayerStateChange 
     } 
    }); 
    } 

    // 4. The API will call this function when the video player is ready. 
    function onPlayerReady(event) { 
    event.target.playVideo(); 
    } 

    // 5. The API calls this function when the player's state changes. 
    // The function indicates that when playing a video (state=1), 
    // the player should play for six seconds and then stop. 
    var done = false; 
    function onPlayerStateChange(event) { 
    if (event.data == YT.PlayerState.PLAYING && !done) { 
     setTimeout(stopVideo, 6000); 
     done = true; 
    } 
    } 
    function stopVideo() { 
    player.stopVideo(); 
    } 
</script> 
0

स्वीकृत उत्तर बहुत अच्छा काम करता है। मैं और अधिक नियंत्रण चाहते थे तो मैं स्क्रिप्ट के लिए और अधिक कार्यों के एक जोड़े कहा:

function unmuteVideo() { 
    player.unMute(); 
    return false; 
    } 
    function muteVideo() { 
    player.mute(); 
    return false; 
    } 
    function setVolumeVideo(volume) { 
    player.setVolume(volume); 
    return false; 
    } 

और यहाँ HTML है:

<br> 
<button type="button" onclick="unmuteVideo();">Unmute Video</button> 
<button type="button" onclick="muteVideo();">Mute Video</button> 
<br> 
<br> 
<button type="button" onclick="setVolumeVideo(100);">Volume 100%</button> 
<button type="button" onclick="setVolumeVideo(75);">Volume 75%</button> 
<button type="button" onclick="setVolumeVideo(50);">Volume 50%</button> 
<button type="button" onclick="setVolumeVideo(25);">Volume 25%</button> 

अब आप ध्वनि के अधिक नियंत्रण है ... अधिक के लिए संदर्भ यूआरएल की जाँच करें:

YouTube IFrame Player API

0

var video1; 
 

 
function onYouTubeIframeAPIReady(){ 
 
\t player = new YT.Player("video1", { 
 
\t \t videoId: "id-number", 
 
\t \t width: 300, 
 
\t \t height: 200, 
 
\t \t playerVars: { 
 
\t \t \t "autoplay": 1, // and 0 means off 
 
\t \t \t "controls": 1, 
 
\t \t \t "showinfo": 0, 
 
\t \t \t "modestbranding": 0, 
 
\t \t \t "loop": 1, 
 
\t \t \t "fs": 0, 
 
\t \t \t "cc_load_policy": 0, 
 
\t \t \t "iv_load_policy": 3, 
 
\t \t \t }, 
 
\t \t events: { 
 
\t \t  'onReady': onPlayerReady 
 
\t \t } 
 
\t }); 
 
    } 
 

 
function onPlayerReady(event) { 
 
    event.target.mute(); 
 
    event.target.setVolume(0); //this can be set from 0 to 100 
 
}

याद रखें कि ध्वनि IE और सफारी में म्यूट नहीं किया जाएगा।

4

इस अपनी कार्यशील ठीक

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

संबंधित मुद्दे

 संबंधित मुद्दे