小程序实现录音功能并播放

小程序实现录音功能并播放

你好朋友们,我是你们的Blogger朋友jahanxah。今天我们一起来在小程序里面实现录音并播放语音资源。本节课用了colorui组建库,感谢colorui。

第一步(写wxml部分)

我们先创建一个audio语音播放组建和一个按钮,为了简单一点我不装修了。audio是原声组建,那个按钮是有bindtouchstartbindtouchend两个属性。bindtouchstart是长按事件的开始(长按开始的时候发生的事件)事件,bindtouchend是长按事件的结束(松开手的发生的事件)事件。

<!–index.wxml–>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<c软件开发定制mhkj33u-custom bgColor=”bg-blue” class=”uy” isCustom=”{{true}}”>

<view slot=”content”>小程序实现录音</view>

</cu-custom>

<view class=”container”>

<view>

<audio src=”{{src}}” controls></audio>

</view>

<view class=”margin-top”>

<button bindtouchstart=”luyin_start” bindtouchend=”luyin_end” class=”cu-btn  bg-blue uy text-xl软件开发定制mhkj33” style=”margin: auto;width: 400rpx;height: 80rpx;”>

请长按说话

</button>

</view>

</view>

第二步  逻辑部分

现在我们一起来实现一下逻辑部分。首先我们要获取使用麦克风的权限,如果授权成功的话开始录音,授权失败的话提示录音失败。

<!–index.wxml–>

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

5软件开发定制mhkj331

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

luyin_start() {

var that = this

const options = {

duration: 600000, //指定录音的时长,单位 ms

sampleRate: 16000, //采样率

numberOfChannels: 1, //录音通道数

encodeBitRate: 96000, //编码码率

format: mp3, //音频格式,有效值 aac/mp3

frameSize: 50, //指定帧大小,单位 软件开发定制mhkj33KB

}

wx.authorize({

scope: scope.record,

success() {

// console.log(“录音授权成功”);

//第一次成功授权后 状态切换为2

that.setData({

status: 2,

})

let luyin_dur = that.data.luyin_dur;

recorderManager.start(options);

// setTimeout(luyin1_dur+1, 1000)

// setInterval(luyin1_dur+1,1000)

recorderManager.onStart(() => {

wx.showToast({

title:软件开发定制mhkj33 正在录音,

icon: loading,

duration: luyin_dur

});

});

//错误回调

recorderManager.onError((res) => {

console.log(res);

})

},

fail() {

console.log(“第一次录音授权失败”);

wx.showModal({

title: 提示,

content: 您未授权录音,功能将无法使用,

showCancel: true,

confirmText: “授权”,

confirmColor: “#52a2d8”,

success: function (res) {

if (res.confirm) {

//确认则打开设置页面软件开发定制mhkj33(重点)

wx.openSetting({

success: (res) => {

console.log(res.authSetting);

if (!res.authSetting[scope.record]) {

//未设置录音授权

console.log(“未设置录音授权”);

wx.showModal({

title: 提示,

content: 您未授权录音,功能将无法使用,

showCancel: false,

success: function (res) {

},

})

} else {

//第二次才成功授权

console.log(“设置录音授权成功”);

that.setData({

status: 2,

})软件开发定制mhkj33

recorderManager.start(options);

recorderManager.onStart(() => {

console.log(recorder start)

});

//错误回调

recorderManager.onError((res) => {

console.log(res);

})

}

},

fail: function () {

console.log(“授权设置录音失败”);

}

})

} else if (res.cancel) {

console.log(“cancel”);

}

},

fail: function () {

console.log(“openfail”);

}

})

}

})

}

软件开发定制mhkj33者最新博文
2022-11-17 19:49:11

© 版权声明
THE END
喜欢就支持一下吧
点赞597 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容