If you are asked to add audio resources to the web page, how will you implement it?
Before the emergence of h5, we can use the iframe element to insert video resources into our web pages. The code is implemented as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <iframe height=498 width=510 src='http://player.youku.com/embed/XMzIzNTc0MTAwMA==' frameborder=0 ' allowfullscreen'></iframe> </body> </html>
The effect is as follows :
In addition, we can also use Html5 audio and video tags to introduce audio media resources into our web pages to increase the richness of the web pages Spend.
The
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>h5 audio标签的使用</title> </head> <body> <audio controls loop> <source src="bgsound.mp3" /> <source src="music.ogg" /> 您的浏览器版本太低 </audio> </body> </html>
The effect is as follows (chrome browser): Adding the controls attribute to the audio tag can display controls to the user, such as the play button; the loop attribute means that playback starts again whenever the audio ends.
The audio tag has different effects under different browsers:
Many trendy websites offer videos, Until now, there was still no standard for displaying videos on web pages. Today, most videos are displayed through plug-ins such as Flash. However, not all browsers have the same plugins.
HTML5 specifies a standard way to include video through the video element. And the video element supports global attributes (such as class, id, title, style, etc.) and event attributes (such as onresize, onredo, etc.) in HTML.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <video autoplay controls> <source src="movie04.ogg" /> <source src="mp4.mp4" /> Opps,您的浏览器版本太低,暂不支持该视频的播放~ </video> </body> </html>
##video Attribute
<video src="test.mp4" controls width="400" style="max-width:90%"></video>
Switch playback Address (commonly used to switch between ultra-clear and high-definition smooth, different video addresses of different quality) <video src="test.mp4" controls autoplay width="400" height="300" id="test1"></video>
<script>
var video = document.getElementById('test1')
console.log(video.src) // http://127.0.0.1:8001/test.mp4 绝对地址,DOM 中是相对地址
// video.src = 'test-2.mp4' // 直接替换掉了原来的视频src
setTimeout(() => {
video.src = 'test-2.mp4' // 播放到第 30s 的时候,自动切换视频
}, 30000)
</script>
Switching alternative addresses, multiple source elements can be embedded in the video tag as playback addresses Backup switching, when the first video fails to load, the next video will be automatically loaded.
## The above is the detailed content of The use of new tags audio and video in h5. For more information, please follow other related articles on the PHP Chinese website! <video controls autoplay width="400" height="300" id="test2">
<source src="test3.mp4" type="video/mp4" />
<source src="test9.mp4" type="video/mp4" />
<source src="test-2.mp4" type="video/mp4" />
</video>
<script>
var video = document.getElementById('test2')
setTimeout(() => {
console.log(video.currentSrc) // http://127.0.0.1:8001/test-2.mp4
}, 1000)
// HTTP 载入失败,状态码 404。媒体资源 http://127.0.0.1:8001/test3.mp4 载入失败。
// HTTP 载入失败,状态码 404。媒体资源 http://127.0.0.1:8001/test9.mp4 载入失败。
</script>