zengh
2022-05-09 52c39f5e2711e5535b689b66dfa5e100c7882b7f
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
class myAudio {
    constructor(arg) {
        // this.id = arg.id;
        // this.src = arg.src;
        this.fn = arg.fn || function(res) {
            // console.log(res)
        }
        //实例化声音  
        // let doit = () => {
        this.state = '1';
        this.uni = arg.uni;
        this.Audio = this.uni.createInnerAudioContext();
        // this.Audio.src = this.src;
        this.Audio.onPlay(() => {
            // console.log("开始播放", this.src);
        })
        this.Audio.onEnded(() => {
            // console.log("音频自然播放结束事件", this.src);
            // doit();
            this.state = '1';
        })
        this.Audio.onTimeUpdate((res) => {
            // console.log("音频播放进度更新事件", res, this.src);
        })
 
        this.Audio.onError((res) => {
            // console.log(res.errMsg);
            // console.log(res.errCode);
        })
        // }
        // doit();
    }
    $play(src) {
        // console.log(this.state)
        if (this.state == '2') {
            //之前有播放 需要先清除
            this.Audio.destroy();
            this.Audio = this.uni.createInnerAudioContext();
        }
        this.Audio.src = '/static/song/' + src + '.mp3';
        this.Audio.play();
        this.state = '2';
    }
    $pause() {
        if (!this.Audio.paused) {
            this.Audio.pause();
            this.state = '2';
        }
    }
    $stop() {
        if (this.Audio) {
            this.Audio.stop();
            this.state = '2';
        }
    }
    $destroy() {
        if (this.Audio) {
            this.Audio.destroy();
            this.state = '2';
        }
    }
}
 
export default myAudio