董国庆
2025-06-06 d0f4c2d4bb7a72b32fb9945de8908e1d4ab6509c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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;
            // 设置音频格式为MPEG
            state.innerAudioContext.format = 'mpeg';
            state.innerAudioContext.src = url;
            
            // 显示音频信息
            // uni.showToast({
            //     title: '音频链接:' + url,
            //     icon: 'none',
            //     duration: 3000
            // });
            
            // 添加加载事件处理
            state.innerAudioContext.onCanplay(() => {
                console.log('音频可以播放了');
                uni.showToast({
                    title: '音频已准备就绪',
                    icon: 'none',
                    duration: 2000
                });
            });
            
            state.innerAudioContext.onPlay(() => {
                state.playFlag = true;
                uni.showToast({
                    title: '开始播放',
                    icon: 'none',
                    duration: 2000
                });
            });
            
            state.innerAudioContext.onError((res) => {
                console.log('播放错误', res);
                uni.showToast({
                    title: '播放错误'+`${res}`,
                    icon: 'none',
                    duration: 3000
                });
                // 显示具体的错误信息
                // let errorMsg = '音频播放失败';
                // if (res.errMsg) {
                //     errorMsg += ':' + res.errMsg;
                // }
                // uni.showToast({
                //     title: errorMsg,
                //     icon: 'none',
                //     duration: 3000
                // });
                dispatch('stopPlaying')
            });
            
            state.innerAudioContext.onEnded((res) => {
                console.log('播放自然结束', res);
                uni.showToast({
                    title: '播放结束',
                    icon: 'none',
                    duration: 2000
                });
                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