import Vue from 'vue'
|
import Vuex from 'vuex'
|
Vue.use(Vuex)
|
|
const store = new Vuex.Store({
|
state: {
|
isList: false,
|
innerAudioContext: null,
|
playFlag: false //是否在播放
|
},
|
mutations: {
|
SET_ISLIST(state, data) {
|
state.isList = data
|
},
|
},
|
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
|