| ¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view style="padding: 40rpx;"> |
| | | <button @click="speak('订åå·²æ¶å°ï¼å¼å§ææ¥')">å¼å§ææ¥</button> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | export default { |
| | | data() { |
| | | return { |
| | | tts: null // åçTTS对象 |
| | | } |
| | | }, |
| | | |
| | | onReady() { |
| | | this.initTTS() // 页颿¸²æå®æåå§åè¯é³ |
| | | }, |
| | | |
| | | methods: { |
| | | // ========== åå§åå®ååçè¯é³ ========== |
| | | initTTS() { |
| | | if (uni.getSystemInfoSync().platform !== 'android') { |
| | | console.log('ä»
æ¯æå®å') |
| | | return |
| | | } |
| | | |
| | | try { |
| | | // 导å
¥å®ååçç±» |
| | | const TextToSpeech = plus.android.importClass('android.speech.tts.TextToSpeech') |
| | | const Locale = plus.android.importClass('java.util.Locale') |
| | | |
| | | // å建TTS |
| | | this.tts = new TextToSpeech(plus.android.runtimeMainActivity(), { |
| | | onInit: (status) => { |
| | | if (status == 0) { |
| | | // è®¾ç½®ä¸æ |
| | | this.tts.setLanguage(Locale.CHINA) |
| | | console.log('è¯é³åå§åæå') |
| | | } |
| | | } |
| | | }) |
| | | } catch (e) { |
| | | console.log('åå§å失败', e) |
| | | } |
| | | }, |
| | | |
| | | // ========== è¯é³ææ¥ï¼æ ¸å¿æ¹æ³ï¼ ========== |
| | | speak(text) { |
| | | if (!this.tts) { |
| | | uni.showToast({ |
| | | title: 'è¯é³æªåå¤å¥½', |
| | | icon: 'none' |
| | | }) |
| | | return |
| | | } |
| | | |
| | | try { |
| | | // å®ååçææ¥ï¼QUEUE_FLUSH = ç«å³ææ¥ï¼ææä¸ä¸æ¡ï¼ |
| | | this.tts.speak(text, 0, null) |
| | | } catch (err) { |
| | | console.log('ææ¥å¤±è´¥', err) |
| | | } |
| | | }, |
| | | |
| | | // åæ¢ææ¥ |
| | | stopSpeak() { |
| | | if (this.tts) this.tts.stop() |
| | | } |
| | | }, |
| | | |
| | | // 页é¢éæ¯æ¶å
³éè¯é³ |
| | | onUnload() { |
| | | if (this.tts) { |
| | | this.tts.stop() |
| | | this.tts.shutdown() |
| | | } |
| | | } |
| | | } |
| | | </script> |