voice.js
2.79 KB
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
var util = require('../../../../util/util.js')
var playTimeInterval
var recordTimeInterval
Page({
data: {
recording: false,
playing: false,
hasRecord: false,
recordTime: 0,
playTime: 0,
formatedRecordTime: '00:00:00',
formatedPlayTime: '00:00:00'
},
onHide: function() {
if (this.data.playing) {
this.stopVoice()
} else if (this.data.recording) {
this.stopRecordUnexpectedly()
}
},
startRecord: function () {
this.setData({ recording: true })
var that = this
recordTimeInterval = setInterval(function () {
var recordTime = that.data.recordTime += 1
that.setData({
formatedRecordTime: util.formatTime(that.data.recordTime),
recordTime: recordTime
})
}, 1000)
wx.startRecord({
success: function (res) {
that.setData({
hasRecord: true,
tempFilePath: res.tempFilePath,
formatedPlayTime: util.formatTime(that.data.playTime)
})
},
complete: function () {
that.setData({ recording: false })
clearInterval(recordTimeInterval)
}
})
},
stopRecord: function() {
wx.stopRecord()
},
stopRecordUnexpectedly: function () {
var that = this
wx.stopRecord({
success: function() {
console.log('stop record success')
clearInterval(recordTimeInterval)
that.setData({
recording: false,
hasRecord: false,
recordTime: 0,
formatedRecordTime: util.formatTime(0)
})
}
})
},
playVoice: function () {
var that = this
playTimeInterval = setInterval(function () {
var playTime = that.data.playTime + 1
console.log('update playTime', playTime)
that.setData({
playing: true,
formatedPlayTime: util.formatTime(playTime),
playTime: playTime
})
}, 1000)
wx.playVoice({
filePath: this.data.tempFilePath,
success: function () {
clearInterval(playTimeInterval)
var playTime = 0
console.log('play voice finished')
that.setData({
playing: false,
formatedPlayTime: util.formatTime(playTime),
playTime: playTime
})
}
})
},
pauseVoice: function () {
clearInterval(playTimeInterval)
wx.pauseVoice()
this.setData({
playing: false
})
},
stopVoice: function () {
clearInterval(playTimeInterval)
this.setData({
playing: false,
formatedPlayTime: util.formatTime(0),
playTime: 0
})
wx.stopVoice()
},
clear: function () {
clearInterval(playTimeInterval)
wx.stopVoice()
this.setData({
playing: false,
hasRecord: false,
tempFilePath: '',
formatedRecordTime: util.formatTime(0),
recordTime: 0,
playTime: 0
})
}
})