play() failed because the user didn't interact with the document first audio 如何控制自動播放


//加上靜音屬性也播放不了
 
<audio
        ref="audio"
        preload="auto"
        autoplay
        src="./tip.mp3"
        controls="controls"
        hidden
        loadedmetadata="onLoadedmetadata"
      />
    </section>
    <button ref="playBtn" @click="startPlay">播放</button>
//添加一個播放按鈕,正常點擊就可以播放  隱藏起來
 
事件:
 startPlay() {
      this.$refs.audio.currentTime = 0 // 從頭開始播放提示音
      this.$refs.audio.play()
    },
 
 
//需要觸發的時候   我的需求是輪播圖到某一張圖片的時候讓播放聲音 下面是輪播的一項配置
 swiperOptionWarning: {
        autoplay: { disableOnInteraction: false }, // 主要處理點擊以后不輪播的bug
        loop: false,
        slidesPerView: 3,
        spaceBetween: 20,
        speed: 800,
        // 用戶操作swiper之后,是否禁止autoplay.默認為true:停止。
        autoplayDisableOnInteraction: false,
        // 拖動釋放時不會輸出信息,阻止click冒泡。拖動Swiper時阻止click事件。
        preventLinksPropagation: true,
        // 設置點擊箭頭
        navigation: {
          nextEl: '#next1',
          prevEl: '#prev1'
        }, on: {
          slideChangeTransitionStart: function() {
       cleartimeout()
            if (_this.warningItems[this.activeIndex - 1].new) {
              setTimeout(() => {
                    _this.$refs.playBtn.click()                                  //定時器,這樣就行 主動書法點擊事件
              })
            
            }
          }
        }
      },
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM