[TTS] uni-app voice broadcast app development/MT-TTS Android native speech synthesis plug-in (free and unlimited)

What is TTS?

TTS is Text To Speechthe abbreviation of " Text to Speech ", which is a part of human-machine dialogue that allows machines to speak. TTS is a type of speech synthesis application.

In program development, there are many TTS plug-ins, such as Baidu, iFlytek, etc., but most of the products are not completely free with unlimited calls and need to be used online.

This article introduces a free and fast method that can be used offline and locally: calling the TTS installed on the Android system for speech synthesis playback.

Use of Android’s native offline speech synthesis

1. Install Android text-to-speech engine

The Android system itself has its own speech engine, but some mobile phones do not have it. If not, you need to install the speech engine apk first. You can refer to this article to download: Simple comparison and download of Android text-to-speech engine (TTS)

2. The calling system has installed TTS plug-in (MT-TTS)

As long as there is already a speech engine on the mobile phone, you can use the MT-TTS plug-in to call it.

2.1 Download MT-TTS plug-in

Download address and usage: MT-TTS offline speech synthesis

You can develop directly on the downloaded demo, or copy the folder in the demo nativepluginsto the root directory of your own project.
Insert image description here
Then open the project with HBuilder X, click manifest.jsonFile, App native plug-in configuration, check MT-TTS, and confirm.

Insert image description here

2.2 Use a custom base to package uni native plug-ins (Note: Please use a real machine to run the custom base)

To use uni's native plug-in, you must first submit it for cloud packaging before it can take effect, so you need to package the native plug-in into a custom base first, and then write the code for real-machine debugging (if you don't know how to do real-machine debugging, you can read my article on real-machine debugging ) debugging ).
Run -> Run to mobile phone or simulator -> Make a custom debugging base
Insert image description here
package
Insert image description here

Package and Release
After using a custom base to develop and debug the uni-app native plug-in, you cannot directly release the custom base apk as the official version.
Cloud packaging should be resubmitted ("Custom base" cannot be checked) to generate an official version.
After the custom base is packaged, select the custom debugging base.

Insert image description here
After that, you can run and debug it on a real machine.
Insert image description here

2.3 Official use of MT-TTS

There is sample code in the demo of the plug-in, which is quite detailed. Here is a brief explanation of the main steps:
import requireNativePluginthe plug-in

const SpeechTTS= uni.requireNativePlugin("MT-TTS-Speech");

Initialize first

<button class="title" size="mini" @click="init">Init TTS</button>
init() {
    
    
		console.log('>> TTS:init...')
		SpeechTTS.init((callback) => {
    
    
				this.isReady = true;
				console.log('>> tts: init success');
		});

		SpeechTTS.onDone((res) => {
    
    
				console.log(">> tts: play end " + res)
		});
},

Voice playback

<button class="title" size="mini" @click="play">播放</button>
text: '请。112号、张三,到,C04、检查'
play() {
    
    
		console.log('>> tts: play...');
		const res = SpeechTTS.speak({
    
    
					text: this.text
		});
		console.log('>> tts: play result = ' + res);
},

For other uses, please refer to the plug-in API below.

Plugin API

  • initializationinit(options, callback)
  • Set the intonation setPitch(num) range 0-9, default 5
  • Set speech rate setSpeed(num)range 0-9, default 5
  • Set speech rate setVolumn(num) range 0-9, default 15
  • Get status isSpeeking()
  • play speek(text, utteranceId)
  • Play changeSpeeker(text)text: F (Du Xiaomei), M (Du Xiaoyu), Y (Du Yaya), X (Du Xiaoyao), return 0 successfully
  • Stop synthesisstop()
  • Pause playbackpaused()
  • Resume playbackresume()
  • destroydestroy()
  • Start playback callback registration(callback)
  • Complete playback callback registrationonDone(callback)
  • Playback error callback registration (callback)

That’s it for this article. I wish all programmers a happy 1024 Programmer’s Day!
Insert image description here

Guess you like

Origin blog.csdn.net/qq_23073811/article/details/127486966