readContent.vue 7.8 KB

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