reading.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. Page({
  2. data: {
  3. fullScreenBtn: false,
  4. playBtn: false,
  5. gesture: true,
  6. muted: true,
  7. gesture: false,
  8. centerBtn: false,
  9. recordFlag: 0,
  10. recordSource: '',
  11. videoCtr: 'recordingVideoEnd',
  12. },
  13. onLoad: function (option) {
  14. if (option.title) {
  15. wx.setNavigationBarTitle({
  16. title: option.title //页面标题为路由参数
  17. })
  18. }
  19. this.recorderManager = wx.getRecorderManager();
  20. this.videoCtx = wx.createVideoContext('myVideo', this);
  21. // 监听录音部分
  22. // 录音开始
  23. this.recorderManager.onStart(() => {
  24. this.videoCtx.play();
  25. this.setData({
  26. recordFlag: 1
  27. })
  28. console.log('recorder start')
  29. })
  30. // 录音结束
  31. this.recorderManager.onStop((res) => {
  32. this.videoCtx.stop();
  33. console.log('recorder stop', res)
  34. console.log(res.tempFilePath)
  35. const recordFile = res.tempFilePath;
  36. this.setData({
  37. recordFlag: 0,
  38. recordSource: recordFile
  39. })
  40. })
  41. },
  42. // onShow:function(){
  43. // },
  44. // 录音中视频播放结束 (控制录音同时结束)
  45. recordingVideoEnd:function(){
  46. //
  47. if (this.data.recordFlag === 0) {
  48. this.recordStop();
  49. this.setData({
  50. videoCtr : 'playingVideoEnd'
  51. })
  52. }
  53. // 录音结束
  54. if (this.data.recordFlag === 1) {
  55. // this.recordStop();
  56. }
  57. },
  58. // 播放中视频播放结束 (控制录音同时结束)
  59. playingVideoEnd:function(){
  60. this.innerAudioContext.stop();
  61. },
  62. /***
  63. * recordFlag:
  64. * 0 初始状态
  65. * 1 录音中
  66. ***/
  67. audioRecord: function () {
  68. console.log(this.data.recordFlag)
  69. if (this.data.recordFlag === 0) {
  70. this.recordStart();
  71. }
  72. // 录音结束后
  73. if (this.data.recordFlag === 1) {
  74. this.recordStop();
  75. }
  76. },
  77. // 录音开始
  78. /**
  79. * duration: 时长 最长10分钟
  80. sampleRate: 44100, 采样率
  81. numberOfChannels: 1, 录音通道
  82. encodeBitRate: 192000, 码率
  83. format: 'mp3', 格式
  84. frameSize: 50 制定帧大小
  85. */
  86. recordStart: function () {
  87. const options = {
  88. duration: 600000,
  89. sampleRate: 44100,
  90. numberOfChannels: 1,
  91. encodeBitRate: 192000,
  92. format: 'mp3',
  93. frameSize: 50
  94. }
  95. this.recorderManager.start(options);
  96. },
  97. // 录音结束
  98. recordStop: function () {
  99. this.recorderManager.stop();
  100. },
  101. // 播放录音
  102. audioPlay: function () {
  103. console.log('音频播放');
  104. this.innerAudioContext = wx.createInnerAudioContext();
  105. this.innerAudioContext.onError((res) => {
  106. // 播放音频失败的回调
  107. })
  108. this.innerAudioContext.src = this.data.recordSource; // 这里可以是录音的临时路径
  109. this.videoCtx.play();
  110. this.innerAudioContext.play();
  111. },
  112. })