hejianhao
4 天以前 60bdd6e881fbefc8c375eb9e9d71009d6c63859d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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