Intelligent dialogue APP based on Aispeech (4) -- external TTS engine and voice broadcast, setting speaker, setting speech speed and other functions
- 1, Problems and Solutions
- 2, External TTS engine
- 2.1 TTS engine
- 2.2 voice broadcast priority
- 2.3 stop broadcasting
- 2.4 setting up TTS engine
- 2.5 setting the speaker
- 2.6 setting the speaking speed
- 2.7 setting volume
- 2.8 set the channel for TTS broadcast
- 2.9 focus of equipment
- 2.10 setting TTS staffing
- 2.11 obtaining synthetic tone information
- 2.12 setting the delay time after the end of TTS
- 3, Code download
In this blog post, I share my problems and how to solve them in the process of learning Aispeech. I will upload the code at the end of the article. Interested partners, please download by yourself.
1, Problems and Solutions
First of all, I want to share the problems I encountered when implementing this function.
Error reporting: import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity;
resolvent:
import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity;
2, External TTS engine
2.1 TTS engine
If you use a third-party TTS engine, you can register the third-party TTS engine with DDS through the following methods.
public void setTTSListener() { try { DDS.getInstance().getAgent().getTTSEngine().setListener(new TTSEngine.Callback() { /** * Callback at start of synthesis * @param ttsId The id of the current TTS. The broadcast ttsid in the conversation process is 0 by default. For the broadcast called through the speak interface, the ttsid is specified by the speak interface. */ @Override public void beginning(String ttsId) { Log.d(TAG, "TTS Start broadcasting ttsId = " + ttsId); } /** * The callback of the synthesized audio data may be returned multiple times. A data length of 0 indicates the end of the audio * @param data Audio data */ @Override public void received(byte[] data) { Log.d(TAG, "When audio is received, this method calls back several times until data 0, audio end data = " + data.length); } /** * TTS Callback of broadcast completion * @param status Status of the end of broadcast. * The end of normal broadcast is 0 * The broadcast was interrupted and ended with 1 */ @Override public void end(String ttsId, int status) { Log.d(TAG, "TTS End of broadcast status = " + status + ", ttsId = " + ttsId); } /** * Bad callback during composition * @param error error message */ @Override public void error(String error) { Log.d(TAG, "There was an error," + error); } }); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
2.2 voice broadcast priority
Priority code of voice broadcast:
public void speakTextWithPriorityOne() { try { DDS.getInstance().getAgent().getTTSEngine().speak("hello, nice to meet you", 1, "10001", AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void speakTextWithPriorityTwo() { try { DDS.getInstance().getAgent().getTTSEngine().speak("Artificial intelligence( Artificial Intelligence),Abbreviated as AI. It is a new technology science to research and develop the theory, method, technology and application system for simulating, extending and expanding human intelligence.", 2, "10002", AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void speakTextWithPriorityThree() { try { DDS.getInstance().getAgent().getTTSEngine().speak("The machine used to study the main material basis of AI and realize the AI technology platform is computer. The development history of AI is connected with the development history of computer science and technology. In addition to computer science, artificial intelligence also involves information theory, cybernetics, automation, bionics, biology, psychology, mathematical logic, linguistics, medicine, philosophy and other disciplines. The main contents of artificial intelligence research include: knowledge representation, automatic reasoning and search methods, machine learning and knowledge acquisition, knowledge processing system, natural language understanding, computer vision, intelligent robot, automatic programming and so on.", 3, "10003", AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void speakSSML() { //SSML broadcast String test1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<speak>\n" + " Founded in 2007 in Cambridge high tech Zone, the founders are from Cambridge, and returned to Suzhou in 2008.\n" + " \n" + " <prosody volume=\"x-loud\"> \n" + " " + "It is one of the few companies with independent property rights and integrated Chinese and English Speech Technology (speech recognition, speech synthesis, natural language understanding, intelligent interactive decision-making, voiceprint recognition, gender and age recognition, emotion recognition, etc.) in the world with man-machine dialogue technology,\n" + " </prosody> \n" + " \n" + " <prosody rate=\"x-slow\">\n" + " Its speech recognition, voiceprint recognition, oral dialogue system and other technologies have won the championship in the evaluation of the National Bureau of standards, the United States Department of defense and international research institutions for many times,\n" + " </prosody>\n" + " <prosody pitch=\"+5%\">\n" + " Represents the international frontier level of Technology,\n" + " </prosody>\n" + " \n" + " <prosody pitch=\"-10%\">\n" + " It is rated as a high-tech enterprise by the Chinese and British governments.\n" + " </prosody>\n" + " \n" + "</speak>"; try { DDS.getInstance().getAgent().getTTSEngine().speak(test1, 1, "ssml"); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
2.3 stop broadcasting
Stop broadcast code:
public void shupWithTTSId() { try { DDS.getInstance().getAgent().getTTSEngine().shutup("10001"); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void shupWithZero() { try { DDS.getInstance().getAgent().getTTSEngine().shutup("0"); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void shupWithNULL() { try { DDS.getInstance().getAgent().getTTSEngine().shutup(""); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
2.4 setting up TTS engine
Set TTS engine code:
public void setLocal() { try { DDS.getInstance().getAgent().getTTSEngine().setMode(TTSEngine.LOCAL); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setCloud() { try { DDS.getInstance().getAgent().getTTSEngine().setMode(TTSEngine.CLOUD); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
2.5 setting up speakers
Set speaker Code:
public void setSpeaker1() { try { DDS.getInstance().getAgent().getTTSEngine().setSpeaker("zhilingf"); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setSpeaker2() { try { DDS.getInstance().getAgent().getTTSEngine().setSpeaker("gdgm"); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setSpeaker3() { try { DDS.getInstance().getAgent().getTTSEngine().setSpeaker("feyinf"); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setSpeaker4() { try { DDS.getInstance().getAgent().getTTSEngine().setSpeaker("boy"); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setSpeaker5() { try { DDS.getInstance().getAgent().getTTSEngine().setSpeaker(null); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setCustomSpeaker() { //Set speaker try { DDS.getInstance().getAgent().getTTSEngine().setSpeaker("geyou", "sdcard/geyoum_common_back_ce_local.v2.1.0.bin"); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
2.6 set speed
Set speed code:
public void setSpeed() { try { DDS.getInstance().getAgent().getTTSEngine().setSpeed(0.8f); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setSpeed1() { try { DDS.getInstance().getAgent().getTTSEngine().setSpeed(1.5f); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
2.7 setting volume
Set volume Code:
public void setVolume() { try { DDS.getInstance().getAgent().getTTSEngine().setVolume(10); toMain(); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setVolume1() { try { DDS.getInstance().getAgent().getTTSEngine().setVolume(1); toMain(); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setVolume2() { try { DDS.getInstance().getAgent().getTTSEngine().setVolume(50); toMain(); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
2.8 set the channel for TTS broadcast
Set the channel code of TTS broadcast:
public void setStreamType() { try { DDS.getInstance().getAgent().getTTSEngine().setStreamType(AudioManager.STREAM_RING); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setStreamType1() { try { DDS.getInstance().getAgent().getTTSEngine().setStreamType(AudioManager.STREAM_MUSIC); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setStreamType2() { try { DDS.getInstance().getAgent().getTTSEngine().setStreamType(AudioManager.STREAM_ALARM); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
2.9 focus of equipment
Equipment focus Code:
public void enableFocus() { try { DDS.getInstance().getAgent().getTTSEngine().enableFocus(true); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void disableFocus() { try { DDS.getInstance().getAgent().getTTSEngine().enableFocus(false); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
2.10 setting TTS personnel
To set TTS personnel Code:
public void setStyle() { /** * Set TTS settings * @param style Style, humor: humor; calm; common: ordinary; short: short; */ try { DDS.getInstance().getAgent().getTTSEngine().setStyle("humor"); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void removeStyle() { try { DDS.getInstance().getAgent().getTTSEngine().removeStyle(); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
2.11 obtaining synthetic tone information
Get polyphonic information code:
//Returns the currently used polyphonic type of String, such as "zhilingf". null is returned if the acquisition fails public void getSpeaker() { try { String speaker = DDS.getInstance().getAgent().getTTSEngine().getSpeaker(); Log.d(TAG,"speaker:"+speaker); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } //Returns the current composite speech speed of float, with a return value of 0.5-2.0, with the fastest speed of 0.5 and the slowest speed of 2.0. If the acquisition fails, it returns 0 public void getSpeed() { try { float speed = DDS.getInstance().getAgent().getTTSEngine().getSpeed(); Log.d(TAG,"speed:"+speed); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } //Return int current synthetic tone volume, return value 1-100, get failure return 0 public void getVolume() { try { int volume = DDS.getInstance().getAgent().getTTSEngine().getVolume(); Log.d(TAG,"volume:"+volume); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } //Return to the list of current customized broadcast audio of list < customaudiobean > public void getCustomAudio() { try { List customAudio = DDS.getInstance().getAgent().getTTSEngine().getCustomAudio(); Log.d(TAG,"customAudio:"+customAudio); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
2.12 set delay time after TTS
Set delay time code after TTS end:
public void setPlayAfterTime() { //v1.2.3.1 try { DDS.getInstance().getAgent().getTTSEngine().setPlayAfterTime(2000); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } } public void setPlayAfterTime1() { //v1.2.3.1 try { DDS.getInstance().getAgent().getTTSEngine().setPlayAfterTime(0); } catch (DDSNotInitCompleteException e) { e.printStackTrace(); } }
3, Code download
If there is a problem that cannot be solved or a small partner who is interested in the code, you can download it directly DUIdemo2.4.zip.