readContent2.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. <!-- 音标区 -->
  8. <view class="yb-play-box">
  9. <text>{{data.yinbiao}}</text>
  10. <icon class="yb-play-btn"></icon>
  11. <icon class="yb-playing-btn" v-if="false"></icon>
  12. </view>
  13. <view style="text-align: center;">
  14. <text v-if="data.jianyi&&data.jianyi.length>0" v-for="(item,index) in data.jianyi" :key="index">
  15. {{item}}
  16. </text>
  17. </view>
  18. <view>
  19. <view class="audio-play-btn"></view>
  20. <view class="audio-playing-btn" v-if="false"></view>
  21. </view>
  22. <view class="container">
  23. <view class="status">{{ recordingStatus }}</view>
  24. <view class="duration" v-if="isRecording">录音时长: {{ Math.floor(duration) }}秒</view>
  25. <button class="record-btn" :class="{ recording: isRecording, disabled: isPlaying }"
  26. @touchstart="handleTouchStart" @touchend="handleTouchEnd" @touchcancel="handleTouchEnd"
  27. @mousedown="handleMouseDown" @mouseup="handleMouseUp" @mouseleave="handleMouseLeave"
  28. :disabled="isPlaying">
  29. {{ isRecording ? '松开结束' : '按住录音' }}
  30. <view v-if="isPlaying" class="disabled-mask">播放中不可录音</view>
  31. </button>
  32. <button class="play-btn" :class="{ disabled: isRecording || !voicePath }" @click="playVoice"
  33. :disabled="isRecording || !voicePath">
  34. {{ isPlaying ? '播放中...' : '播放录音' }}
  35. <view v-if="isRecording" class="disabled-mask">录音中不可播放</view>
  36. </button>
  37. <view class="tips">提示:长按按钮录音,松开结束(需3秒以上)</view>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <script setup>
  43. import selectTypesVue from './selectTypes.vue';
  44. import {
  45. onLoad
  46. } from "@dcloudio/uni-app"
  47. import {
  48. reactive,
  49. ref,
  50. onMounted,
  51. onUnmounted
  52. } from 'vue';
  53. const props = defineProps({
  54. activeWord: {
  55. type: Object,
  56. },
  57. pageData: {
  58. type: Object,
  59. },
  60. activeWords: {
  61. type: Array
  62. },
  63. })
  64. let tabFlag = ref(1)
  65. const audioInfo = ref(null)
  66. const data = reactive({
  67. name: '',
  68. yinbiao: '',
  69. jianyi: []
  70. })
  71. // 录音相关状态
  72. const isRecording = ref(false)
  73. const isPlaying = ref(false)
  74. const voicePath = ref('')
  75. const duration = ref(0)
  76. const timer = ref(null)
  77. const recordingStatus = ref('准备就绪')
  78. const startTime = ref(0)
  79. const longPressTimer = ref(null) // 长按计时器
  80. const MIN_RECORD_TIME = 3 // 最小录音时间3秒
  81. // 获取录音和音频播放管理器
  82. const recorderManager = uni.getRecorderManager()
  83. const innerAudioContext = uni.createInnerAudioContext()
  84. // 初始化录音器
  85. const initRecorder = () => {
  86. recorderManager.onStart(() => {
  87. console.log('recorder start')
  88. isRecording.value = true
  89. recordingStatus.value = '录音中...'
  90. startTime.value = Date.now()
  91. startTimer()
  92. })
  93. recorderManager.onStop((res) => {
  94. console.log('recorder stop', res)
  95. const recordTime = (Date.now() - startTime.value) / 1000
  96. stopTimer()
  97. isRecording.value = false
  98. if (recordTime < MIN_RECORD_TIME) {
  99. recordingStatus.value = '录音时间太短(需3秒以上)'
  100. uni.showToast({
  101. title: '录音时间太短,需3秒以上',
  102. icon: 'none'
  103. })
  104. return
  105. }
  106. if (res.tempFilePath) {
  107. voicePath.value = res.tempFilePath
  108. recordingStatus.value = `录音完成,时长: ${Math.floor(recordTime)}秒`
  109. uni.showToast({
  110. title: '录音成功',
  111. icon: 'success'
  112. })
  113. } else {
  114. recordingStatus.value = '录音失败'
  115. uni.showToast({
  116. title: '录音失败',
  117. icon: 'none'
  118. })
  119. }
  120. })
  121. recorderManager.onError((res) => {
  122. console.error('recorder error', res)
  123. stopTimer()
  124. isRecording.value = false
  125. recordingStatus.value = `录音错误: ${res.errMsg}`
  126. uni.showToast({
  127. title: `录音出错: ${res.errMsg}`,
  128. icon: 'none'
  129. })
  130. })
  131. innerAudioContext.onPlay(() => {
  132. isPlaying.value = true
  133. recordingStatus.value = '播放中...'
  134. })
  135. innerAudioContext.onEnded(() => {
  136. isPlaying.value = false
  137. recordingStatus.value = '播放完成'
  138. })
  139. innerAudioContext.onError((res) => {
  140. isPlaying.value = false
  141. console.error('play error', res)
  142. uni.showToast({
  143. title: '播放失败',
  144. icon: 'none'
  145. })
  146. })
  147. }
  148. // 检查权限
  149. const checkPermission = () => {
  150. uni.authorize({
  151. scope: 'scope.record',
  152. success: () => {
  153. console.log('已授权录音权限')
  154. },
  155. fail: (err) => {
  156. console.log('未授权录音权限', err)
  157. uni.showModal({
  158. title: '提示',
  159. content: '需要录音权限才能使用此功能',
  160. confirmText: '去设置',
  161. success: (res) => {
  162. if (res.confirm) {
  163. uni.openSetting()
  164. }
  165. }
  166. })
  167. }
  168. })
  169. }
  170. // 处理触摸开始(移动端)
  171. const handleTouchStart = (e) => {
  172. e.preventDefault()
  173. if (isPlaying.value) return
  174. // 设置长按计时器,500ms后才开始录音
  175. longPressTimer.value = setTimeout(() => {
  176. startRecording()
  177. }, 400)
  178. }
  179. // 处理触摸结束(移动端)
  180. const handleTouchEnd = (e) => {
  181. e.preventDefault()
  182. clearTimeout(longPressTimer.value)
  183. if (isRecording.value) {
  184. endRecording()
  185. }
  186. }
  187. // 处理触摸取消(移动端,如被系统中断)
  188. const handleTouchCancel = (e) => {
  189. handleTouchEnd(e) // 与touchend同样处理
  190. }
  191. // 处理鼠标按下(桌面端)
  192. const handleMouseDown = (e) => {
  193. e.preventDefault()
  194. if (isPlaying.value) return
  195. longPressTimer.value = setTimeout(() => {
  196. startRecording()
  197. }, 500)
  198. }
  199. // 处理鼠标抬起(桌面端)
  200. const handleMouseUp = (e) => {
  201. e.preventDefault()
  202. clearTimeout(longPressTimer.value)
  203. if (isRecording.value) {
  204. endRecording()
  205. }
  206. }
  207. // 处理鼠标移出按钮区域
  208. const handleMouseLeave = (e) => {
  209. clearTimeout(longPressTimer.value)
  210. if (isRecording.value) {
  211. endRecording()
  212. }
  213. }
  214. // 开始录音
  215. const startRecording = () => {
  216. if (isRecording.value) return
  217. console.log('开始录音')
  218. recordingStatus.value = '准备录音...'
  219. isRecording.value = true // 提前设置状态,避免延迟
  220. startTime.value = Date.now()
  221. startTimer()
  222. const options = {
  223. duration: 60000,
  224. sampleRate: 44100,
  225. numberOfChannels: 1,
  226. encodeBitRate: 192000,
  227. format: uni.getSystemInfoSync().platform === 'android' ? 'amr' : 'mp3',
  228. frameSize: 50
  229. }
  230. recorderManager.start(options)
  231. }
  232. // 结束录音
  233. const endRecording = () => {
  234. if (!isRecording.value) return
  235. console.log('停止录音')
  236. isRecording.value = false
  237. stopTimer()
  238. recorderManager.stop()
  239. }
  240. // 播放录音
  241. const playVoice = () => {
  242. if (isRecording.value || !voicePath.value) return
  243. console.log('播放录音:', voicePath.value)
  244. innerAudioContext.src = voicePath.value
  245. innerAudioContext.play()
  246. }
  247. // 计时器相关
  248. const startTimer = () => {
  249. duration.value = 0
  250. timer.value = setInterval(() => {
  251. duration.value += 1 // 每秒增加1,而不是0.1
  252. }, 1000) // 间隔改为1000ms
  253. }
  254. const stopTimer = () => {
  255. if (timer.value) {
  256. clearInterval(timer.value)
  257. timer.value = null
  258. }
  259. duration.value = 0
  260. }
  261. // 初始化单词数据
  262. const initItem = () => {
  263. data.name = props.activeWord.name;
  264. data.yinbiao = props.activeWord.yinbiao;
  265. data.jianyi = props.activeWord.jianyi;
  266. }
  267. onMounted(() => {
  268. initRecorder()
  269. checkPermission()
  270. initItem()
  271. })
  272. onUnmounted(() => {
  273. stopTimer()
  274. clearTimeout(longPressTimer.value)
  275. recorderManager.stop()
  276. innerAudioContext.stop()
  277. })
  278. </script>
  279. <style>
  280. .container {
  281. padding: 20px;
  282. display: flex;
  283. flex-direction: column;
  284. align-items: center;
  285. }
  286. .status {
  287. margin: 20px 0;
  288. color: #666;
  289. font-size: 16px;
  290. min-height: 24px;
  291. }
  292. .duration {
  293. margin-bottom: 20px;
  294. color: #333;
  295. font-weight: bold;
  296. }
  297. .record-btn {
  298. background-color: #4CAF50;
  299. color: white;
  300. margin: 10px 0;
  301. width: 80%;
  302. padding: 15px 0;
  303. border: none;
  304. border-radius: 5px;
  305. position: relative;
  306. }
  307. .record-btn.recording {
  308. background-color: #F44336;
  309. }
  310. .play-btn {
  311. background-color: #2196F3;
  312. color: white;
  313. margin: 10px 0;
  314. width: 80%;
  315. padding: 15px 0;
  316. border: none;
  317. border-radius: 5px;
  318. }
  319. .disabled {
  320. opacity: 0.7;
  321. position: relative;
  322. }
  323. .disabled-mask {
  324. position: absolute;
  325. top: 0;
  326. left: 0;
  327. right: 0;
  328. bottom: 0;
  329. background: rgba(0, 0, 0, 0.5);
  330. color: white;
  331. display: flex;
  332. align-items: center;
  333. justify-content: center;
  334. font-size: 14px;
  335. }
  336. .tips {
  337. margin-top: 20px;
  338. color: #999;
  339. font-size: 12px;
  340. text-align: center;
  341. }
  342. </style>