readContent.vue 7.7 KB

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