馃uniapp 灏忕▼搴忓浣曞疄鐜颁腑鑻辨枃浜哄0鏈楄鍔熻兘锛� oil娆у摕 2024-12-09 269 闃呰5鍒嗛挓 鍓嶈█ 鏈�杩戝湪鎴戠殑瀛︿範鍗$洅灏忕▼搴忎腑澧炲姞浜嗕竴涓崱鐗囦汉澹版湕璇荤殑鏁堟灉锛岀偣鍑诲彸灏忚鐨勬挱鏀炬寜閽氨浼氭挱鏀惧崱鐗囦腑鐨勬枃瀛楀搴旂殑浜哄0鏈楄鏁堟灉銆傛晥鏋滃涓嬪浘锛�/p> 涓昏鐨勫師鐞嗘槸鐢ㄥ埌浜嗗皬绋嬪簭鐨勫悓澹颁紶璇戞彃浠讹紝涓嬮潰灏辫缁嗚璁插浣曞皢杩欎釜鎻掍欢闆嗘垚锛屼互鍙婁氦浜掑簲璇ユ�庝箞鍋�. 寮曠敤鎻掍欢 寰俊鐨�a href="http://222.178.203.72:19005/whst/63/=khmjzitdihmzbm>sZqfds=gssor$29$1E$1Ecdudknodqrzvdhwhmzppzbnl$1ElhmhoqnfqZl$1Ecdu$1EokZsenql-bZoZahkhshdr$1Edwsdmcdc$1EsqZmrkZsnqzgslk/" target="_blank" title="https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/extended/translator.html" ref="nofollow noopener noreferrer">鍚屽0浼犺瘧鎻掍欢鏈変笁涓姛鑳斤紝鍒嗗埆鏄� 璇煶杈撳叆锛�strong>鏂囨湰缈昏瘧鍜�strong>璇煶鍚堟垚锛堟枃瀛楄浆璇煶锛�/strong>锛屾垜鐢ㄥ埌鐨勫氨鏄闊冲悎鎴愬姛鑳姐��/p> 棣栧厛寰俊鏈嶅姟甯傚満涓櫥褰曞苟娣诲姞鍚屽0浼犺瘧鎻掍欢锛屾垜鏄釜浜鸿处鍙峰悓鏍峰彲浠ョ敵璇凤紝閾炬帴鍦ㄨ繖閲岋細fuwu.weixin.qq.com/service/det鈥�/a> 娣诲姞瀹屾垚鍚庡湪灏忕▼搴忓悗鍙� mp.weixin.qq.com/ 鐐瑰嚮宸︿笅瑙掔殑璐﹀彿璁剧疆 椤堕儴 Tab 鏍忛�夋嫨绗笁鏂硅缃紝鍦ㄦ彃浠剁鐞嗕腑鎵惧埌鍚屽0浼犺瘧鎻掍欢锛岀偣鍑昏鎯�/p> 鍦ㄨ鎯呬腑鍙互鏌ョ湅鎻掍欢鐨� APPID锛屾彁鍓嶅鍒跺ソ 鎺ヤ笅鏉ュ湪灏忕▼搴忛」鐩殑 manifest.json 鏂囦欢涓坊鍔犱竴涓彃浠跺紩鐢細 "mp-weixin": { "appid": "wxff30426f8850b0d7", "setting": { "urlCheck": false }, "usingComponents": true, + "plugins": { + "WechatSI": { + "provider": "涓婁竴姝ュ鍒剁殑 APPID", + "version": "0.3.6" } } }, 濡傛鎿嶄綔锛屾彃浠跺氨绠楁槸鍦ㄩ」鐩腑鎴愬姛寮曠敤浜嗐��/p> 鍔熻兘浣跨敤 棣栧厛锛屽湪浠g爜涓紩鐢ㄦ彃浠� const plugin = requirePlugin('WechatSI') 涓嬩竴姝ュ皾璇曡皟鐢ㄤ竴涓� API 鐪嬬湅鑳戒笉鑳借皟閫氾細 plugin?.textToSpeech({ content, lang: 'zh_CN', success: (res) => { audio.src = res.filename audio.play() }, fail: (err) => { console.log('err: ', err) uni.showToast({ icon: 'none', title: '鏈楄澶辫触锛岃绋嶅悗閲嶈瘯', }) }, 杩欓噷鐨� content 鏄枃鏈唴瀹癸紝lang 鏄瑷�锛屾洿澶氬弬鏁板彲浠ユ煡鐪嬫枃妗o紝濡傛灉鑳藉椤哄埄璋冮�氬氨鍙互寮�濮嬪皾璇曟惌閰嶅姛鑳戒娇鐢ㄣ�備笂闈㈢殑浠g爜鍙互鐪嬪埌锛岃皟鐢ㄦ垚鍔熻繑鍥炰簡涓�涓煶棰戞枃浠剁殑鍦板潃锛屽洜姝ゆ垜浠渶瑕佹挱鏀捐繖涓煶棰戞枃浠躲��/p> const audio = uni.createInnerAudioContext() audio.src = res.filename audio.play() 闊抽鐨勬挱鏀鹃渶瑕佺敤鍒板皬绋嬪簭鐨� createInnerAudioContext API 鍘诲垱寤洪煶棰戜笂涓嬫枃瀹炰緥锛岀劧鍚庝慨鏀瑰疄渚嬬殑 src 灞炴�ф寚瀹氭挱鏀鹃煶棰戠殑閾炬帴锛屽湪鎵ц play() 灏卞彲浠ュ皢闊抽鎾斁銆�/p> 瀹屾暣鐨勯厤缃ぇ瀹跺彲浠ュ弬鑰冩垜鐨勪唬鐮佺ず渚嬶紝涓婃墜鍗崇敤锛�/p> const onPlaying = ref(false) // 鎾斁鐘舵�侊紝true琛ㄧず姝e湪鎾斁锛宖alse琛ㄧず鏈挱鏀�/span> const onAudioLoading = ref(false) // 鍔犺浇鐘舵�侊紝true琛ㄧず姝e湪鍔犺浇闊抽锛宖alse琛ㄧず鍔犺浇瀹屾垚鎴栨湭寮�濮嬪姞杞�/span> // 浣跨敤reactive鏉ュ垱寤轰竴涓搷搴斿紡瀵硅薄锛岀敤浜庣紦瀛橀煶棰戞枃浠剁殑鍚嶇О const audioCacheFileName = reactive({}) // 鍒涘缓涓�涓唴閮ㄩ煶棰戜笂涓嬫枃锛岀敤浜庢帶鍒堕煶棰戠殑鎾斁銆佸仠姝㈢瓑 const audio = uni.createInnerAudioContext() // 浣跨敤lodash鐨刣ebounce鍑芥暟鏉ラ槻鎶栧鐞嗙偣鍑讳簨浠讹紝纭繚鍦ㄧ煭鏃堕棿鍐呭娆$偣鍑诲彧鎵ц涓�娆℃搷浣�/span> const onSpeakBtnClick = _.debounce(() => { // 濡傛灉褰撳墠姝e湪鎾斁闊抽锛屽垯鍋滄鎾斁 if (onPlaying.value) { audio.stop() return } // 鏍规嵁褰撳墠鍗$墖绱㈠紩鍜岀炕杞姸鎬佽幏鍙栬鏈楄鐨勫唴瀹�/span> const content = cardItemList.value[currentCardIndex.value]?.[ isCardFlipped(currentCardIndex.value) ? 'backContent' : 'frontContent' ]?.trim() // 妫�鏌ョ紦瀛樹腑鏄惁宸插瓨鍦ㄨ鍐呭鐨勯煶棰戞枃浠�/span> if (audioCacheFileName[content]) { // 濡傛灉瀛樺湪锛屽垯鐩存帴浣跨敤缂撳瓨鐨勯煶棰戞枃浠�/span> audio.src = audioCacheFileName[content] audio.play() return } // 璁剧疆鍔犺浇鐘舵�佷负true锛岃〃绀哄紑濮嬪姞杞介煶棰�/span> onAudioLoading.value = true // 璋冪敤鏂囨湰杞闊虫彃浠讹紝灏嗘枃鏈浆鎹负璇煶 plugin?.textToSpeech({ content, // 瑕佽浆鎹㈢殑鏂囨湰鍐呭 lang: 'zh_CN', // 浣跨敤鐨勮瑷�锛岃繖閲屾槸涓浗澶ч檰鏅�氳瘽 success: (res) => { // 鎴愬姛鑾峰彇闊抽鏂囦欢鍚庯紝璁剧疆闊抽婧愪负杞崲鍚庣殑鏂囦欢锛屽苟缂撳瓨鏂囦欢鍚�/span> audio.src = res.filename audioCacheFileName[content] = res.filename audio.play() // 鎾斁闊抽 }, fail: (err) => { // 杞崲澶辫触鏃讹紝杈撳嚭閿欒淇℃伅骞舵樉绀烘彁绀�/span> console.log('err: ', err) uni.showToast({ icon: 'none', title: '鏈楄澶辫触锛岃绋嶅悗閲嶈瘯', }) }, complete: () => { // 鏃犺鎴愬姛鎴栧け璐ワ紝瀹屾垚鍚庨兘灏嗗姞杞界姸鎬佽缃负false onAudioLoading.value = false }, }) }, 300) // 闃叉姈鏃堕棿闂撮殧璁剧疆涓�0姣 // 鐩戝惉闊抽鎾斁浜嬩欢锛屽綋闊抽寮�濮嬫挱鏀炬椂锛屽皢鎾斁鐘舵�佽缃负true audio.onPlay(() => { onPlaying.value = true }) 娉ㄦ剰鐐�/h3> 澶у鍙互鐪嬪埌鎴戠殑浠g爜绀轰緥涓寚瀹氫簡璇█涓轰腑鏂囷紝鍏跺疄鎴戠殑灏忕▼搴忎腑鏄棦鏈変腑鏂囦篃鏈夎嫳鏂囩殑锛屽湪鏈�寮�濮嬫垜鑷繁瀹炵幇浜嗕竴涓垽鏂瓧绗︿覆涓嫳鏂囧拰绗﹀彿鐨勫嚱鏁帮紝浣嗘槸鍚庨潰鍙戠幇鏃犺鏄腑鏂囪繕鏄嫳鏂囷紝鎸囧畾涓轰腑鏂囬兘鍙互姝e父鏈楄锛岀渷鍘讳簡寰堝鍒ゆ柇锛岃�屼笖涓枃鐨勪汉澹版洿濂藉惉锛岄煶璐ㄤ篃鏇村ソ锛屽鏋滄寚瀹氳嫳鏂囪鍑烘潵鐨勬満姊版劅寰堥噸锛屾墍浠ヨ繖閲屽ぇ瀹朵笉蹇呰姳蹇冩�濆幓鍒ゆ柇璇█绫诲瀷銆�/p> API 鐨勮皟鐢ㄦ槸鏈夐檺棰濈殑锛屽洜姝ゆ挱鏀炬寜閽渶濂藉仛涓槻鎶栧鐞嗐�傞櫎浜嗛槻鎶栵紝鍦ㄦ垜鐨勪唬鐮佷腑鎴戣繕绠�鍗曞疄鐜颁簡涓�涓紦瀛樻満鍒讹紝褰撴鏂囨湰鎾斁杩囦箣鍚庡氨浼氭斁鍒� audioCacheFileName 瀵硅薄涓紝姣忔鎾斁鏃讹紝鍏堝姣斾竴涓嬪綋鍓嶇殑鏂囨湰鏄笉鏄凡缁忔湕璇昏繃浜嗭紝濡傛灉宸茬粡鏈楄杩囦簡锛屽苟涓斿唴瀹规病鏈変慨鏀瑰氨鐩存帴鎾斁宸茬粡璇锋眰杩囩殑閭d釜闊抽锛岃繖鏍峰彲浠ュぇ澶у噺灏� API 鐨勮皟鐢ㄦ鏁般��/p> 鎬荤粨 寰俊灏忕▼搴忕殑鏈楄鍔熻兘瀹炵幇杩樻槸姣旇緝绠�鍗曠殑锛岃窡鐫�姝ラ璧颁竴涓皬鏃跺氨鍋氬ソ浜嗐�傚鏋滄枃绔犲浣犳湁甯姪璇峰府鎴戠偣涓禐锛岀洰鍓嶅皬绋嬪簭宸茬粡涓婄嚎锛屽皬绋嬪簭鍚嶇О涓� 鈥滃涔犲崱鐩掆��/strong>锛岀传鑹插浘鏍囬偅涓紝宸茬粡鍦ㄩ�氳繃璁よ瘉浜嗭紝鍙互鍦ㄥ崱鐗囧唴浣撻獙涓�涓嬫湕璇荤殑鏁堟灉銆�/p>