[HTML]audio音频标签的属性和事件

/ 32,277评论 / 110771阅读 / 2点赞

<audio>是HTML5新加入的用于播放音频的标签,HTML5已经得到广泛的支持,所以我们可以放心使用这个标签;利用它,可以轻松地在网页上播放音频,制作音乐播放器等。下面我们来聊聊它的属性和事件

概念

元数据


属性

兼容性提示

<audio>
  能看到这段话,说明您的浏览器不支持 audio 标签捏
</audio>

controls默认音乐控制面板

<audio controls>
  能看到这段话,说明您的浏览器不支持 audio 标签捏
</audio>
<audio controls="controls">
  能看到这段话,说明您的浏览器不支持 audio 标签捏
</audio>

src音频文件链接

<audio src="./song.mp3" controls>
  能看到这段话,说明您的浏览器不支持 audio 标签捏
</audio>

source资源文件标签


<audio controls>
    <source src="./song.ogg" type="audio/ogg">
    <source src="./song.mp3" type="audio/mpeg">
    能看到这段话,说明您的浏览器不支持 audio 标签捏
</audio>

autoplay自动播放

<audio autoplay>
  您的浏览器不支持 audio 标签捏
</audio>
<audio autoplay="autoplay">
  您的浏览器不支持 audio 标签捏
</audio>

loop开启循环

<audio loop src="./song.mp3">
  您的浏览器不支持 audio 标签捏
</audio>

muted静音

<audio muted src="./song.mp3">
  您的浏览器不支持 audio 标签捏
</audio>

preload预加载方式

<audio preload="auto" src="./song.mp3">
  您的浏览器不支持 audio 标签捏
</audio>

JS操作属性

只读

这部分属性只能读取,不能进行赋值修改

duration音频总时长

const audioDOM = document.getElementById("audio");
if (isNaN(audioDOM.duration)) {
    // 未加载时的操作
} else {
    // 已加载时的操作
}

paused是否暂停

const audioDOM = document.getElementById("audio");
if (audioDOM.paused == true) {  //如果是暂停状态
    audioDOM.play();  //播放音频
} else {              //如果正在播放
    audioDOM.pause(); //暂停
}

ended是否播放完毕

currentSrc音频链接

buffered已缓存范围

let audio = document.getElementById("audio");
let timeRanges = audio.buffered;
let num = 0;
/*获取已加载时长,然后除以歌曲时长,即可得到已加载比例*/
if(timeRanges.length > 0)
    num = parseInt(timeRanges.end(timeRanges.length - 1) * 100 / audio.duration);
if (num > 100)
    num = 100;
else if (num < 0)
    num = 0;

seekable可跳转范围

networkState网络范围

error发生错误

seeking是否移动或跳转播放进度

<audio id="audio" controls src="song.mp3" onseeking="seeking">
  您的浏览器不支持 audio 标签捏
</audio>

<script>
seeking:function(){
    const audioDOM = document.getElementById("audio");
    console.log(audioDOM.seeking);
}
</script>

可读写

这部分属性可以读取,也可以赋值修改

currentTime已播放时长

const audioDOM = document.getElementById("audio");
if (audioDOM.currentTime > 5) {
    audioDOM.currentTime -= 5;
} else {
    audioDOM.currentTime = 0;
}

volume音量

const audioDOM = document.getElementById("audio");
if (audioDOM.volume < 0.9) {
    audioDOM.volume += 0.1;
} else {
    audioDOM.volume = 1;
}

playbackRate播放速度

const audioDOM = document.getElementById("audio");
if (audioDOM.playbackRate >= 2)
    audioDOM.playbackRate = 0.5;
else
    audioDOM.playbackRate += 0.25;

标签属性控制

以下属性都会返回布尔值boolean,可读写设置对应的标签属性是否启用

const audioDOM = document.getElementById("audio");
audioDOM.controls = true;
if(audioDOM.loop == false){
    audioDOM.loop = true;
}

JS操作函数

play()播放

const audioDOM = document.getElementById("audio");
if (audioDOM.canPlay == true) {   //如果资源准备好可以播放
    if (audioDOM.paused == true)  //音频为暂停状态
        audioDOM.play();    //播放
    else
        audioDOM.pause();   //暂停
}

pause()暂停

load()加载


事件

加载生命周期

loadstart开始加载

durationchange时长改变

loadedmetadata元数据加载完毕

loadeddata数据开始加载

progress加载中

canplay可以播放

canplaythrough可以播放整个音频

其他事件

timeupdate已播放时长更新

waiting等待资源中

playing播放中

play播放

pause暂停

ended结束

volumechange音量改变

ratechange播放速率改变

error发生错误

  1. pechi_aoEr说道:

    банная печь атмосфера купить https://pechka-atmosfera.ru/ .

  2. LarryGok说道:

    PBN sites
    We build a system of privately-owned blog network sites!

    Benefits of our privately-owned blog network:

    WE DO everything so Google doesn’t comprehend THAT this A self-owned blog network!!!

    1- We buy domain names from different registrars

    2- The leading site is hosted on a virtual private server (VPS is fast hosting)

    3- The rest of the sites are on separate hostings

    4- We assign a unique Google profile to each site with verification in Google Search Console.

    5- We design websites on WP, we don’t use plugins with assistance from which malware penetrate and through which pages on your websites are created.

    6- We refrain from duplicate templates and utilise only individual text and pictures

    We refrain from work with website design; the client, if wished, can then edit the websites to suit his wishes

  3. RickyAlage说道:

    adderall canadian pharmacy canadian pharmacies canadian pharmacy drugs online

  4. LarryGok说道:

    PBN sites
    We generate a structure of privately-owned blog network sites!

    Pros of our private blog network:

    We carry out everything SO THAT Google DOES NOT comprehend THAT this is A self-owned blog network!!!

    1- We buy domain names from distinct registrars

    2- The principal site is hosted on a virtual private server (Virtual Private Server is fast hosting)

    3- Additional sites are on separate hostings

    4- We attribute a unique Google profile to each site with verification in Google Search Console.

    5- We make websites on WordPress, we don’t use plugins with assisted by which Trojans penetrate and through which pages on your websites are created.

    6- We never repeat templates and utilise only individual text and pictures

    We never work with website design; the client, if wanted, can then edit the websites to suit his wishes

  5. Michaelsew说道:

    https://mexicoph24.life/# buying prescription drugs in mexico

  6. LarryGok说道:

    PBN sites
    We’ll create a structure of private blog network sites!

    Merits of our self-owned blog network:

    WE DO everything SO THAT google DOES NOT understand that this A privately-owned blog network!!!

    1- We obtain web domains from different registrars

    2- The leading site is hosted on a VPS server (Virtual Private Server is fast hosting)

    3- The remaining sites are on separate hostings

    4- We assign a separate Google profile to each site with confirmation in Google Search Console.

    5- We design websites on WP, we don’t utilize plugins with aided by which malware penetrate and through which pages on your websites are created.

    6- We don’t reproduce templates and employ only distinct text and pictures

    We refrain from work with website design; the client, if desired, can then edit the websites to suit his wishes

  7. Marcelcrolf说道:

    https://mexicoph24.life/# medication from mexico pharmacy

  8. Marcelcrolf说道:

    https://canadaph24.pro/# canadian pharmacy meds

  9. Michaelsew说道:

    http://canadaph24.pro/# best canadian online pharmacy

  10. amarkhauri说道:

    Зайдите на сайт FishX https://fishx.org/ и вы найдете все о рыбалке, cнастях, прикормке, приманках, способах ловле, а также отчёты о рыбной ловле. Ознакомьтесь с видами рыб и на что они ловятся, их места обитания. Сайт будет полезен как начинающим рыбакам, так и профи с опытом рыбной ловли.

  11. LarryGok说道:

    PBN sites
    We’ll generate a web of PBN sites!

    Benefits of our PBN network:

    We execute everything SO THAT Google DOES NOT understand that THIS IS A privately-owned blog network!!!

    1- We purchase web domains from different registrars

    2- The principal site is hosted on a VPS server (Virtual Private Server is fast hosting)

    3- Other sites are on distinct hostings

    4- We attribute a individual Google account to each site with confirmation in Google Search Console.

    5- We develop websites on WP, we don’t use plugins with assistance from which Trojans penetrate and through which pages on your websites are created.

    6- We refrain from reiterate templates and use only individual text and pictures

    We refrain from work with website design; the client, if desired, can then edit the websites to suit his wishes

  12. JesusZesia说道:

    казахстан – россия перевозки https://perevozka-iz-kz.ru/

  13. StevenSen说道:

    buying prescription drugs in mexico online: purple pharmacy mexico price list – reputable mexican pharmacies online

  14. LarryGok说道:

    PBN sites
    We will create a structure of privately-owned blog network sites!

    Advantages of our private blog network:

    WE DO everything SO THAT google doesn’t understand THAT this A PBN network!!!

    1- We purchase domains from various registrars

    2- The primary site is hosted on a VPS hosting (Virtual Private Server is rapid hosting)

    3- The rest of the sites are on distinct hostings

    4- We assign a individual Google account to each site with confirmation in Search Console.

    5- We make websites on WordPress, we don’t use plugins with assistance from which Trojans penetrate and through which pages on your websites are produced.

    6- We refrain from duplicate templates and utilize only individual text and pictures

    We don’t work with website design; the client, if wished, can then edit the websites to suit his wishes

回复 http://bit.ly/ 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注