Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use java to realize the function of text-to-voice broadcasting

Shulou Source: shulou.com Published: 2022-06-03 09:25:35 10月02日 Update

Editor to share with you how to use java to achieve text-to-voice broadcasting function, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The method is as follows

1. Pom.xml introduces jar package dependency

Com.hynnet jacob 1.18

Copy the jacob-1.18-x64.dll file to the bin directory of the jdk installation location.

Https://files.cnblogs.com/files/w1441639547/jacob-1.18-x64.rar

Third, import the jacobtest.java class into the project for test run.

Package com.Interface.util;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant / * * text-to-speech test jdk bin file needs to import jacob-1.17-M2-x64.dll * * @ author zk * @ date: 10:05:21 on June 25, 2019 * / public class jacobtest {/ * Voice to text and play * * @ param txt * / public static void textToSpeech (String text) {ActiveXComponent ax = null; try {ax = new ActiveXComponent ("Sapi.SpVoice") / / output voice content Dispatch spVoice = ax.getObject (); / / Volume 0-100 ax.setProperty ("Volume", new Variant (100)); / / speech reading speed-10 to + 10 ax.setProperty ("Rate", new Variant (- 2)); / / perform Dispatch.call (spVoice, "Speak", new Variant (text)) / / the following is the build file stream to generate voice files ax = new ActiveXComponent ("Sapi.SpFileStream"); Dispatch spFileStream = ax.getObject (); ax = new ActiveXComponent ("Sapi.SpAudioFormat"); Dispatch spAudioFormat = ax.getObject (); / / set the audio stream format Dispatch.put (spAudioFormat, "Type", new Variant (22)) / / set the file output stream format Dispatch.putRef (spFileStream, "Format", spAudioFormat); / / call the output file stream opening method to create a .wav file Dispatch.call (spFileStream, "Open", new Variant (". / text.wav"), new Variant (3), new Variant (true)) / / set the audio output stream of the sound object to the output file object Dispatch.putRef (spVoice, "AudioOutputStream", spFileStream); / / set the volume from 0 to 100 Dispatch.put (spVoice, "Volume", new Variant (100)); / / set the reading speed Dispatch.put (spVoice, "Rate", new Variant (- 2)) / / start reading Dispatch.call (spVoice, "Speak", new Variant (text)); / / close the output file Dispatch.call (spFileStream, "Close"); Dispatch.putRef (spVoice, "AudioOutputStream", null); spAudioFormat.safeRelease (); spFileStream.safeRelease (); spVoice.safeRelease (); ax.safeRelease ();} catch (Exception e) {e.printStackTrace () The above is "how to use java to achieve text-to-voice broadcasting function" all the content of this article, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: File voice output text content article function object method format speed volume audio test run not much location sound most more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi Docker Shulou Technology Huawei