readContent.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <template>
  2. <view>
  3. <selectTypesVue activeSelect="3"></selectTypesVue>
  4. <view class="words-xuan-box">
  5. <!-- 单词区 -->
  6. <view class="show-words-box"> {{data.name}} </view>
  7. <view style="text-align: center;">
  8. <text v-if="data.jianyi&&data.jianyi.length>0" v-for="(item,index) in data.jianyi" :key="index">
  9. {{item}}
  10. </text>
  11. </view>
  12. <view class="yb-play-box xue-yb-play-box">
  13. <yinbiaoTxtVue :yinbiao="activeWord.yinbiao"></yinbiaoTxtVue>
  14. <!-- 音频播放 -->
  15. <audioTwoVue :active-word="activeWord" @play-audio="handlePlay"></audioTwoVue>
  16. </view>
  17. <view class="container">
  18. <!-- <view class="status">{{ recordingStatus }}</view> -->
  19. <!-- <view class="duration" v-if="isRecording">录音时长: {{ Math.floor(duration) }}秒</view> -->
  20. <button class="record-btn" :class="{ recording: isRecording, disabled: isPlaying }"
  21. @touchstart="handleTouchStart" @touchend="handleTouchEnd" @touchcancel="handleTouchEnd"
  22. :disabled="isPlaying">
  23. {{ isRecording ? '松开结束' : '按住录音' }}
  24. <!-- <view v-if="isPlaying" class="disabled-mask">播放中不可录音</view> -->
  25. </button>
  26. <button class="play-btn" @click="playVoice" v-if="voicePath">
  27. 播放小喇叭
  28. </button>
  29. <!-- <button class="play-btn" :class="{ disabled: isRecording || !voicePath }" @click="playVoice"
  30. :disabled="isRecording || !voicePath">
  31. {{ isPlaying ? '播放中...' : '播放录音' }}
  32. <view v-if="isRecording" class="disabled-mask">录音中不可播放</view>
  33. </button> -->
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script setup>
  39. import selectTypesVue from './selectTypes.vue';
  40. import audioTwoVue from './audioTwo.vue';
  41. import yinbiaoTxtVue from "./yinbiaoTxt.vue"
  42. import {
  43. onLoad
  44. } from "@dcloudio/uni-app"
  45. import {
  46. reactive,
  47. ref,
  48. onMounted,
  49. onUnmounted
  50. } from 'vue';
  51. const props = defineProps({
  52. activeWord: {
  53. type: Object,
  54. },
  55. pageData: {
  56. type: Object,
  57. },
  58. activeWords: {
  59. type: Array
  60. },
  61. })
  62. const emits = defineEmits(['play-audio'])
  63. let tabFlag = ref(1)
  64. const audioInfo = ref(null)
  65. const data = reactive({
  66. name: '',
  67. yinbiao: '',
  68. jianyi: []
  69. })
  70. // 录音相关状态
  71. const isRecording = ref(false)
  72. const isPlaying = ref(false)
  73. const voicePath = ref('')
  74. const duration = ref(0)
  75. const timer = ref(null)
  76. const recordingStatus = ref('准备就绪')
  77. const startTime = ref(0)
  78. const longPressTimer = ref(null) // 长按计时器
  79. // 获取录音和音频播放管理器
  80. const recorderManager = uni.getRecorderManager()
  81. const innerAudioContext = uni.createInnerAudioContext()
  82. // 初始化录音器
  83. const initRecorder = () => {
  84. recorderManager.onStart(() => {
  85. console.log('recorder start')
  86. isRecording.value = true
  87. // recordingStatus.value = '录音中...'
  88. // startTime.value = Date.now()
  89. // startTimer()
  90. })
  91. recorderManager.onStop((res) => {
  92. // const recordTime = (Date.now() - startTime.value) / 1000 // 计算实际录音时间
  93. isRecording.value = false
  94. // if (recordTime < 3) {
  95. // uni.showToast({
  96. // title: '录音时间太短,需3秒以上',
  97. // icon: 'none'
  98. // })
  99. // return
  100. // }
  101. if (res.tempFilePath) {
  102. voicePath.value = res.tempFilePath
  103. uni.showToast({
  104. title: '录音成功',
  105. icon: 'success'
  106. })
  107. } else {
  108. uni.showToast({
  109. title: '录音失败',
  110. icon: 'none'
  111. })
  112. }
  113. })
  114. recorderManager.onError((res) => {
  115. console.error('recorder error', res)
  116. //stopTimer()
  117. isRecording.value = false
  118. //recordingStatus.value = `录音错误: ${res.errMsg}`
  119. uni.showToast({
  120. title: `录音出错: ${res.errMsg}`,
  121. icon: 'none'
  122. })
  123. })
  124. innerAudioContext.onPlay(() => {
  125. isPlaying.value = true
  126. //recordingStatus.value = '播放中...'
  127. })
  128. innerAudioContext.onEnded(() => {
  129. isPlaying.value = false
  130. //recordingStatus.value = '播放完成'
  131. })
  132. innerAudioContext.onError((res) => {
  133. isPlaying.value = false
  134. console.error('play error', res)
  135. uni.showToast({
  136. title: '播放失败',
  137. icon: 'none'
  138. })
  139. })
  140. }
  141. async function handlePlay(opt) {
  142. emits('play-audio', opt)
  143. }
  144. // 检查权限
  145. const checkPermission = () => {
  146. uni.authorize({
  147. scope: 'scope.record',
  148. success: () => {
  149. console.log('已授权录音权限')
  150. },
  151. fail: (err) => {
  152. console.log('未授权录音权限', err)
  153. uni.showModal({
  154. title: '提示',
  155. content: '需要录音权限才能使用此功能',
  156. confirmText: '去设置',
  157. success: (res) => {
  158. if (res.confirm) {
  159. uni.openSetting()
  160. }
  161. }
  162. })
  163. }
  164. })
  165. }
  166. // 处理触摸开始(移动端)
  167. const handleTouchStart = (e) => {
  168. e.preventDefault()
  169. if (isPlaying.value) return
  170. // 设置长按计时器,500ms后才开始录音
  171. longPressTimer.value = setTimeout(() => {
  172. startRecording()
  173. }, 400)
  174. }
  175. // 处理触摸结束(移动端)
  176. const handleTouchEnd = (e) => {
  177. e.preventDefault()
  178. clearTimeout(longPressTimer.value)
  179. if (isRecording.value) {
  180. endRecording()
  181. }
  182. }
  183. // 处理触摸取消(移动端,如被系统中断)
  184. const handleTouchCancel = (e) => {
  185. handleTouchEnd(e) // 与touchend同样处理
  186. }
  187. // 开始录音
  188. const startRecording = () => {
  189. if (isRecording.value) return
  190. console.log('开始录音')
  191. //recordingStatus.value = '准备录音...'
  192. isRecording.value = true // 提前设置状态,避免延迟
  193. // startTime.value = Date.now()
  194. // startTimer()
  195. const options = {
  196. duration: 60000,
  197. sampleRate: 44100,
  198. numberOfChannels: 1,
  199. encodeBitRate: 192000,
  200. format: 'mp3',
  201. frameSize: 50
  202. }
  203. recorderManager.start(options)
  204. }
  205. // 结束录音
  206. const endRecording = () => {
  207. if (!isRecording.value) return
  208. console.log('停止录音')
  209. isRecording.value = false
  210. //stopTimer()
  211. recorderManager.stop()
  212. }
  213. const getRecordingDuration = () => {
  214. if (!isRecording.value) return 0
  215. return Math.floor((Date.now() - startTime.value) / 1000)
  216. }
  217. // 播放录音
  218. const playVoice = () => {
  219. if (isRecording.value || !voicePath.value) return
  220. console.log('播放录音:', voicePath.value)
  221. innerAudioContext.src = voicePath.value
  222. innerAudioContext.play()
  223. }
  224. // // 计时器相关
  225. // const startTimer = () => {
  226. // duration.value = 0
  227. // const start = Date.now()
  228. // timer.value = setInterval(() => {
  229. // // 计算实际经过的时间(毫秒),然后转换为秒
  230. // const elapsed = Math.floor((Date.now() - start) / 1000)
  231. // duration.value = elapsed
  232. // }, 200) // 缩短检查间隔,但计算基于实际时间差
  233. // }
  234. // const stopTimer = () => {
  235. // if (timer.value) {
  236. // clearInterval(timer.value)
  237. // timer.value = null
  238. // }
  239. // duration.value = 0
  240. // }
  241. // 初始化单词数据
  242. const initItem = () => {
  243. data.name = props.activeWord.name;
  244. data.yinbiao = props.activeWord.yinbiao;
  245. data.jianyi = props.activeWord.jianyi;
  246. console.log('data',data);
  247. }
  248. onMounted(() => {
  249. initItem()
  250. initRecorder()
  251. checkPermission()
  252. })
  253. onUnmounted(() => {
  254. //stopTimer()
  255. clearTimeout(longPressTimer.value)
  256. recorderManager.stop()
  257. innerAudioContext.stop()
  258. })
  259. </script>