mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
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
import {
  getRealPath
} from '../util'
 
import {
  invoke
} from '../../bridge'
 
const RECORD_TIME = 60 * 60 * 1000
 
let recorder
let recordTimeout
 
export function startRecord (args, callbackId) {
  recorder && recorder.stop()
  recorder = plus.audio.getRecorder()
  recorder.record({
    filename: '_doc/audio/',
    format: 'aac'
  }, (res) => {
    invoke(callbackId, {
      errMsg: 'startRecord:ok',
      tempFilePath: res
    })
  }, (res) => {
    invoke(callbackId, {
      errMsg: 'startRecord:fail'
    })
  })
  recordTimeout = setTimeout(() => {
    recorder.stop()
    recorder = false
  }, RECORD_TIME)
}
 
export function stopRecord () {
  if (recorder) {
    recordTimeout && clearTimeout(recordTimeout)
    recorder.stop()
    return {
      errMsg: 'stopRecord:ok'
    }
  }
  return {
    errMsg: 'stopRecord:fail'
  }
}
 
let player
let playerFilePath
let playerStatus
 
export function playVoice ({
  filePath
} = {}, callbackId) {
  if (player && playerFilePath === filePath && playerStatus === 'pause') { // 如果是当前音频被暂停,则继续播放
    playerStatus = 'play'
    player.play((res) => {
      player = false
      playerFilePath = false
      playerStatus = false
      invoke(callbackId, {
        errMsg: 'playVoice:ok'
      })
    })
    return {
      errMsg: 'playVoice:ok'
    }
  }
  if (player) { // 如果存在音频播放,则停止
    player.stop()
  }
  playerFilePath = filePath
  playerStatus = 'play'
  player = plus.audio.createPlayer(getRealPath(filePath))
  // 播放操作成功回调
  player.play((res) => {
    player = false
    playerFilePath = false
    playerStatus = false
    invoke(callbackId, {
      errMsg: 'playVoice:ok'
    })
  })
}
 
export function pauseVoice () {
  if (player && playerStatus === 'play') {
    player.pause()
    playerStatus = 'pause'
  }
  return {
    errMsg: 'pauseVoice:ok'
  }
}
 
export function stopVoice () {
  if (player) {
    player.stop()
    player = false
    playerFilePath = false
    playerStatus = false
  }
  return {
    errMsg: 'stopVoice:ok'
  }
}