数据改变 音频开启与关闭

这个博客展示了在 Vue.js 中如何实现音频播放的自动化控制。代码中定义了一个 `autoPlay` 方法用于播放音频,并在 `wsAdding` 和 `isAudio` 的变化时触发播放。同时,`closePlay` 方法用于暂停并清除音频。音频文件路径为 `click.mp3`,并且在组件挂载后尝试播放。博客还涉及到Vuex状态管理,监听 `wsAdding` 和 `isAudio` 状态来决定何时播放音频。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//audio

<template>
  <div style="position:absolute;z-index:1">
    <!-- {{ wsAdding }} -->
    <audio
      src="../../../public/static/click.mp3"
      id="myaudio"
      ref="fhshj"
      controls="controls"
      loop="false"
      hidden="true"
    ></audio>
      <!-- hidden="true" -->
    <!-- <input type="button" ref="fhshj" @click="autoPlay" value="播放" />
    <input type="button" @click="closePlay" value="关闭" /> -->
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions, mapMutations } =
  createNamespacedHelpers("homedetail");
export default {
  data() {
    return{
      rootDom: null
    }
  },

  mounted(){
    // this.autoPlay()
    // this.$refs.fhshj.click()
  },

  methods: {
    autoPlay() {
      if(this.rootDom){
        this.rootDom.play();
        setTimeout(() => {
          this.closePlay();
        }, 300);
      }else{
          this.rootDom = document.getElementById("myaudio");
          console.log(this.rootDom,'this.rootDom99999999999999')
          this.rootDom.play();
          setTimeout(() => {
            this.closePlay();
          }, 300);
      }

    },
    closePlay() {
      this.rootDom = document.getElementById("myaudio");
      this.rootDom.pause();
      this.rootDom.load();
    },
  },

    computed: {
      ...mapState({
          wsAdding: (state) => state.wsAdding,
          isAudio: (state) => state.isAudio,
      })
    },
    watch:{
      wsAdding:{
        handler(newVal){
          console.log(newVal)
        //  this.$nextTick(() => {this.autoPlay()})
          this.autoPlay()
        },
        // immediate:true,
        // deep:true
      },
      isAudio:{
        handler(newVal){
          console.log(newVal,'newVal')
          this.autoPlay()
        }
      }
    }
};
</script>

<style>
</style>

 

//vuex


        isAudio:false

        set_isAudio(state,info){
            state.isAudio = !state.isAudio
        }

监听isaudio使音频播放

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值