Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Simple usage of sapi phonetic Reading in unity

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you the simple usage of sapi pronunciation in unity. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Running environment: win10, Unity5.6.2f1/Unity2018.2.6f1 (both versions have been tried, OK)

Using SpeechLib;//... SpVoice voice = new SpVoice ()

The book is followed by the sapi of Windows in the Unity above for voice reading.

The code in the above example is only the simplest way to use it, and it is problematic in practice.

It's so direct.

Voice.Speak (str)

The simple and rough use of the above will jam the main thread of the current Unity program and stay stuck until the reading is finished, including closing the Unity program, and still finish reading the content.

First of all, solve the problem of the card main thread.

Naturally, I think of opening child threads.

Thread t=new Thread (() = > {voice.Speak (str);}); t.Start ()

In this way, let it read the text aloud, it really does not block the main line.

However, when you close the Unity program, you will still finish reading the content

Well, make voice stop when you shut it down.

Private void OnApplicationQuit () {voice.Pause ();}

It is really useful, when you close the program, you will not read it aloud immediately, but the program will get stuck for a while before it will be closed.

So there's still a problem.

Final solution:

No need to open child threads

According to my needs, there is no need to pause, just stop, then do not need voice.Pause ()

Code:

Stop reading aloud

Voice.Speak (string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak)

Execute the above statement, stop reading immediately, and call the above statement when the program is closed, such as adding it to the OnApplicationQuit () method.

two。 Do not open child threads to make it difficult to read.

Voice.Speak (str, SpeechVoiceSpeakFlags.SVSFlagsAsync)

So the voice.Speak method, with two parameters, the second parameter is very important.

SVSFPurgeBeforeSpeak

If you are reading aloud, stop and then read the current content

SVSFlagsAsync

This can be seen by the name, asynchronous, will not card the main thread.

In this way, there is no need to open a child thread to read directly asynchronously, the main thread is not stuck, and it can be stopped at any time, and it will not get stuck when closing the Unity program (the released Windows program), and close immediately.

The above is the simple use of sapi voice reading in unity shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report