hejianhao
4 天以前 60bdd6e881fbefc8c375eb9e9d71009d6c63859d
H5/store/index.js
@@ -5,13 +5,70 @@
const store = new Vuex.Store({
   state: {
      isList: false,
      innerAudioContext: null,
      playFlag: false //是否在播放
   },
   mutations: {
      SET_ISLIST(state, data) {
         state.isList = data
      }
      },
   },
   actions: {}
   actions: {
      // 开始播放
      playRecording({
         state,
         dispatch
      }, url) {
         if (state.innerAudioContext) {
            state.innerAudioContext.play()
            return
         }
         state.innerAudioContext = uni.createInnerAudioContext();
         state.innerAudioContext.autoplay = true;
         // state.innerAudioContext.src =
         //    'https://haitunyingyu.obs.cn-southwest-2.myhuaweicloud.com/admin/6660c5497ff34ee5b2bddaed01dd3880.wav';
         state.innerAudioContext.src = url
         state.innerAudioContext.onPlay(() => {
            state.playFlag = true;
         });
         state.innerAudioContext.onError((res) => {
            // console.log('播放错误', res);
            dispatch('stopPlaying')
         });
         state.innerAudioContext.onEnded((res) => {
            // console.log('播放自然结束', res);
            dispatch('stopPlaying')
         });
      },
      // 暂停播放
      pausePlaying({
         state
      }) {
         if (state.innerAudioContext) {
            try {
               state.innerAudioContext.pause();
               state.playFlag = false;
            } catch (e) {
               //TODO handle the exception
            }
         }
      },
      // 停止播放
      stopPlaying({
         state
      }) {
         if (state.innerAudioContext) {
            try {
               state.innerAudioContext.stop();
               state.innerAudioContext.destroy()
               state.innerAudioContext = null
               state.playFlag = false;
            } catch (e) {
               //TODO handle the exception
            }
         }
      }
   }
})
export default store