readContent.vue 7.4 KB

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