管灌系统农户端微信小程序(嘉峪关应用)
zuoxiao
2024-05-27 dc01187c6ca2cf46fef268e84a7ac7fc171f2ebb
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
Page({
  data: {
    recordingSrc: '',
    isRecording: false,
  },
 
  onLoad() {
    this.recorderManager = wx.getRecorderManager();
 
    this.recorderManager.onStart(() => {
      console.log('recorder start');
      this.setData({ isRecording: true });
      this.startWaveformDrawing();
    });
 
    this.recorderManager.onStop((res) => {
      console.log('recorder stop', res);
      const { tempFilePath } = res;
      this.setData({
        recordingSrc: tempFilePath,
        isRecording: false
      });
      this.stopWaveformDrawing();
    });
 
    this.recorderManager.onError((res) => {
      console.error(res);
      this.setData({ isRecording: false });
      this.stopWaveformDrawing();
    });
  },
 
  startRecording() {
    if (this.data.isRecording) return;
 
    wx.authorize({
      scope: 'scope.record',
      success: () => {
        const options = {
          duration: 60000,
          sampleRate: 44100,
          numberOfChannels: 1,
          encodeBitRate: 192000,
          format: 'aac',
          frameSize: 50
        };
        this.recorderManager.start(options);
      },
      fail: () => {
        wx.showModal({
          title: '授权失败',
          content: '请授权录音功能',
          showCancel: false
        });
      }
    });
  },
 
  stopRecording() {
    if (!this.data.isRecording) return;
 
    this.recorderManager.stop();
  },
 
  startWaveformDrawing() {
    if (this.waveformInterval) return;
 
    const canvasContext = wx.createCanvasContext('waveform');
    const drawWaveform = () => {
      if (!this.data.isRecording) return;
 
      // 生成模拟的音量数据
      const data = new Array(100).fill(0).map(() => Math.random() * 100);
 
      canvasContext.clearRect(0, 0, 300, 100);
      canvasContext.beginPath();
      canvasContext.moveTo(0, 50);
      for (let i = 0; i < data.length; i++) {
        const x = (i / data.length) * 300;
        const y = 50 - data[i] / 2;
        canvasContext.lineTo(x, y);
      }
      canvasContext.lineTo(300, 50);
      canvasContext.stroke();
      canvasContext.draw();
 
      this.waveformInterval = setTimeout(drawWaveform, 100);
    };
 
    drawWaveform();
  },
 
  stopWaveformDrawing() {
    clearTimeout(this.waveformInterval);
    this.waveformInterval = null;
  }
});