How to mute an iframe content?

柔情痞子 提交于 2019-12-23 17:26:44

问题


I have a 3rd party iframe containing videos from youtube, vimeo ,...

Is there a generic way to mute the iframe content independently from the video/audio source?


回答1:


Step 1. First you need to access the iframe.

var iframe = document.getElementById('iframeId');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

Step2. Once you get the inner document reference, you can access its inside DOM elements. Suppose your video/audio element id is mediaElem then.

var MediaSource = innerDoc.getElementById('mediaElem');

Step3. Now you could do whatever you want with it. In your case if you want to mute, simply set.

MediaSource.muted=true;



回答2:


You can also, call it and set it's volume to zero or pause it, in javsscript it is something like this: var media = document.getElementById('mediaId'); media.volume = 0; /*set volume to zero|takes values btw 0-1*/ media.pause(); /*pause the audio/video*/



来源:https://stackoverflow.com/questions/33528890/how-to-mute-an-iframe-content

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