| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 | 
							- <template>
 
- 	<view class="content">
 
- 		<view>{{isReady ? 'TTS初始化完成' : '正在初始化...'}}</view>
 
- 		<!-- 已安装的TTS引擎列表 -->
 
- 		<!-- 语调 & 语速 & 合成文本 -->
 
- 		<view style="width: 700rpx;">
 
- 			<view class="uni-form-item uni-column">
 
- 				<view class="title">已安装的TTS引擎列表: </view>
 
- 				<radio-group name="radio" @change="onRadioChange">
 
- 					<label v-for="(item, index) in ttsList" :key="item.value">
 
- 						<radio :value="item.value" :checked="ttsName === item.value" />
 
- 						<text>{{item.name}}</text>
 
- 					</label>
 
- 				</radio-group>
 
- 			</view>
 
- 			<view style="height: 10rpx;"></view>
 
- 			<view class="uni-form-item uni-column">
 
- 				<view class="title">语调: </view>
 
- 				<slider max="100" min="0" :value="pitch" @change="onPicthChange" show-value></slider>
 
- 			</view>
 
- 			<view class="uni-form-item uni-column">
 
- 				<view class="title">语速: </view>
 
- 				<slider max="100" min="0" :value="speed" @change="onSpeedChange" show-value></slider>
 
- 			</view>
 
- 			<view class="uni-form-item uni-column">
 
- 				<view class="title">合成文本: </view>
 
- 				<input class="uni-input" v-model="text" />
 
- 			</view>
 
- 		</view>
 
- 		<view style="height: 20rpx;"></view>
 
- 		<!-- 引擎设置 -->
 
- 		<!-- 播放 -->
 
- 		<view style="display: flex;">
 
- 			<button class="title" size="mini" @click="showToast">Toast</button>
 
- 			<view style="width: 10rpx;"></view>
 
- 			<button class="title" size="mini" @click="init">Init TTS</button>
 
- 			<view style="width: 10rpx;"></view>
 
- 			<button class="title" size="mini" @click="getInstallTTS">获取已安装的TTS</button>
 
- 			<view style="width: 10rpx;"></view>
 
- 			<button class="title" size="mini" @click="play">播放</button>
 
- 			<view style="width: 10rpx;"></view>
 
- 			<button class="title" size="mini" @click="toTestBaidu">测试百度TTS</button>
 
- 		</view>
 
- 	</view>
 
- </template>
 
- <script>
 
- 	const SpeechTTS = uni.requireNativePlugin('MT-TTS-Speech');
 
- 	export default {
 
- 		data() {
 
- 			return {
 
- 				ttsList: [],
 
- 				isReady: false,
 
- 				pitch: 70,
 
- 				speed: 65,
 
- 				ttsName: '',
 
- 				text: '请。112号、张三,到,ABC、检查'
 
- 			}
 
- 		},
 
- 		onLoad() {},
 
- 		onUnload() {
 
- 			SpeechTTS.destroy();
 
- 		},
 
- 		methods: {
 
- 			toTestBaidu() {
 
- 				uni.redirectTo({
 
- 					url: '/pages/baidu/baidu'
 
- 				})
 
- 			},
 
- 			showToast() {
 
- 				SpeechTTS.toast('toast test');
 
- 			},
 
- 			onPicthChange(e) {
 
- 				console.log('>> onPicthChange')
 
- 				this.picth = e.detail.value;
 
- 			},
 
- 			onSpeedChange(e) {
 
- 				console.log('>> onSpeedChange')
 
- 				this.speed = e.detail.value;
 
- 			},
 
- 			onRadioChange(e) {
 
- 				console.log("e.target.value",e.target.value)
 
- 				this.ttsName = e.target.value;
 
- 				console.log('>> set Engine:' + this.ttsName);
 
- 				const res = SpeechTTS.setEngine(this.ttsName);
 
- 				console.log('>> set Engine = ' + res);
 
- 			},
 
- 			init() {
 
- 				console.log('>> TTS:init...')
 
- 				SpeechTTS.init((callback) => {
 
- 					this.isReady = true;
 
- 					this.ttsName = "com.google.android.tts"
 
- 					 SpeechTTS.setEngine(this.ttsName);
 
- 					console.log('>> tts: init success');
 
- 				});
 
- 				SpeechTTS.onDone((res) => {
 
- 					console.log(">> tts: play end " + res)
 
- 				});
 
- 			},
 
- 			play() {
 
- 				console.log('>> tts: play...');
 
- 				console.log('>> pitch: ' + this.pitch)
 
- 				console.log('>> speed: ' + this.speed)
 
- 				console.log('>> text: ' + this.text)
 
- 				SpeechTTS.setPitch(this.pitch);
 
- 				SpeechTTS.setSpeed(this.speed);
 
- 				const res = SpeechTTS.speak({
 
- 					text: this.text
 
- 				});
 
- 				console.log('>> tts: play result = ' + res);
 
- 			},
 
- 			getInstallTTS() {
 
- 				console.log('>> 111111111111111tts: getInstallTTS');
 
- 				SpeechTTS.getInstallTTS(res => {
 
- 					console.log(JSON.stringify(res));
 
- 					if (!res || res.length <= 0) {
 
- 						console.log('>> tts: TTS Engine not found');
 
- 						return
 
- 					}
 
- 					let list = [];
 
- 					res.forEach(v => {
 
- 						list.push({
 
- 							name: v.label,
 
- 							value: v.name,
 
- 						});
 
- 						console.log(v.label + ': ' + v.name)
 
- 					})
 
- 					this.ttsList = list;
 
- 				})
 
- 			},
 
- 		}
 
- 	}
 
- </script>
 
- <style>
 
- 	.content {
 
- 		display: flex;
 
- 		flex-direction: column;
 
- 		align-items: center;
 
- 		justify-content: center;
 
- 	}
 
- 	.tts-item {
 
- 		padding-bottom: 5rpx;
 
- 		border-bottom: 1px solid #3F536E;
 
- 	}
 
- </style>
 
 
  |