前言:工做中有需求,在數據變動有變動時採用聲音提示給用戶,這裏記錄一下。轉載請註明出處:https://www.cnblogs.com/yuxiaole/p/9936180.htmljavascript
網站地址:個人我的vue+element ui demo網站 css
github地址:yuleGH githubhtml
代碼以下(採用 html5 的 audio):vue
<html> <head> <title>測試</title> <!-- 引入樣式 --> <link rel="stylesheet" href="../lib/elementui/theme-chalk/index.css" type="text/css"> </head> <body> <div id="app"> <p style="color: red;">播放聲音提示,不自動播放,點擊按鈕才播放</p> <audio ref="msgTipAudio" controls="controls"> <source src="tip.mp3" type="audio/mpeg"/> Your browser does not support the audio element. </audio> <el-button type="primary" @click="playAudio">播放</el-button> </div> <!-- 引入組件庫 --> <script type="text/javascript" src="../lib/vue.js"></script> <script type="text/javascript" src="../lib/elementui/index.js"></script> <script type="text/javascript"> new Vue({ el: "#app", data: { }, methods: { playAudio(){ this.$refs.msgTipAudio.play().catch(this.catchException); }, catchException: function(exceptionMsg){ var _self = this; console.error(exceptionMsg); _self.$message({ type: 'error', message: '該瀏覽器不支持播放聲音!解決方案:一、點擊該彈出框。或者 一、打開chrome;' + '二、輸入 chrome://flags/#autoplay-policy;三、點擊default,選擇 Setting No user gesture is required;' + '四、重啓chrome;', duration: 0 }); } } }); </script> </body> </html>
轉載請註明出處:https://www.cnblogs.com/yuxiaole/p/9936180.htmlhtml5