Html 在隐藏标签中播放声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15533636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Playing Sound In Hidden Tag
提问by theChampion
I am trying to set sound on web page.
我正在尝试在网页上设置声音。
I found this code. It is working code when the div
is visible but I want to be hidden and working. In this case it is not working because it is hidden with style
attribute. How to make it not visible and playing sound at the same time ?
我找到了这个代码。它是div
可见的工作代码,但我想隐藏并工作。在这种情况下,它不起作用,因为它被style
属性隐藏了。如何使它不可见并同时播放声音?
<div style="display:none">
<embed src="sound.mp3"/>
</div>
回答by couzzi
I agree with the sentiment in the comments above — this canbe pretty annoying. We can only hope you give the user the option to turn the music off.
我同意上面评论中的观点——这可能很烦人。我们只能希望您为用户提供关闭音乐的选项。
However...
然而...
audio { display:none;}
<audio autoplay="true" src="https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg">
The css hides the audio
element, and the autoplay="true"
plays it automatically.
css 隐藏audio
元素,并autoplay="true"
自动播放。
回答by imaguest
<audio autoplay>
<source src="file.mp3" type="audio/mpeg">
</audio>
<audio autoplay>
<source src="file.mp3" type="audio/mpeg">
</audio>
It removes the auto play bar but still plays the music. Invisible sounds!
它删除了自动播放栏,但仍然播放音乐。看不见的声音!
回答by Ruchika BN
I have been trying to attach an audio which should autoplay and will be hidden. It's very simple. Just a few lines of HTML and CSS. Check this out!! Here is the piece of code I used within the body.
我一直在尝试附加一个应该自动播放并且将被隐藏的音频。这很简单。只需几行 HTML 和 CSS。看一下这个!!这是我在正文中使用的一段代码。
<div id="player">
<audio controls autoplay hidden>
<source src="file.mp3" type="audio/mpeg">
unsupported !!
</audio>
</div>
回答by meditat
That's how I achieved it, which is not visible (HORRIBLE SOUND....)
这就是我实现它的方式,这是不可见的(可怕的声音......)
<!-- horrible is your mp3 file name any other supported format.-->
<audio controls autoplay hidden="" src="horrible.mp3" type ="audio/mp3"">your browser does not support Html5</audio>
回答by Zachary Cross
Usually, what I do is put the normal audio tag, and then put autoplay="true"
and I don't add: controls ="true" and that usually works for me
通常,我所做的是放置正常的音频标签,然后放置autoplay="true"
并且我不添加:controls =“true”,这通常对我有用
I put this in a snippet and also made a jsfiddel here
我把它放在一个片段中,并在这里做了一个 jsfiddel
Hope this helps :)
希望这可以帮助 :)
<audio autoplay="true"
src="https://res.cloudinary.com/foxyplays989/video/upload/v1558369838/LetsGo.mp3">
</audio>
回答by user5091678
<audio id="audio" style="display:none;" src="mp3/Fans-Mi-tooXclusive_com.mp3" controls autoplay loop onloadeddata="setHalfVolume()">
This auto-plays, hides the music and reduces the music even if system volume is high to avoid noise. Place this in script:
即使系统音量很高,它也会自动播放,隐藏音乐并减少音乐以避免噪音。把它放在脚本中:
<script>
function setHalfVolume() {
var myAudio = document.getElementById("audio");
myAudio.volume = 0.2;
}
</script>
回答by TheCodingKlam
I hope people would allow them to turn things such as music off, as for button clicks, Sometimes, those are pretty cool. Use the
我希望人们允许他们关闭诸如音乐之类的东西,至于点击按钮,有时,这些很酷。使用
<audio controls autoplay hidden="hidden">
<source src="*file here*" type="*file extension (.mp3 .ogg etc.)*">
<!--This displays an error to users that don't have it supported-->
Your browser does not support the audio element.
</audio>
<audio controls autoplay hidden="hidden">
<source src="*file here*" type="*file extension (.mp3 .ogg etc.)*">
<!--This displays an error to users that don't have it supported-->
Your browser does not support the audio element.
</audio>
As you can see, I don't like to repeat myself much, But I decided with the hidden
tag.
正如你所看到的,我不喜欢重复自己太多,但我决定使用hidden
标签。
Hope this helps.
希望这可以帮助。