Using Jacob in Java for Windows Speech Synthesis and Audio File Generation
This guide walks through downloading Jacob's DLL and JAR, configuring the Java environment, setting up an Eclipse project, and writing Java code that leverages the SAPI COM interfaces to synthesize Chinese text into a WAV file on Windows, complete with step‑by‑step screenshots and a full source example.
Scenario: generate an audio file on a server from Chinese text by invoking Windows speech synthesis through the Jacob COM bridge.
First, download the Jacob DLL and JAR from the provided link, unzip the archive, and locate the required jacob.dll and jacob.jar files.
Copy the two DLL files into the Java runtime's bin directory, for example C:\Program Files\Java\jdk1.8.0_241\bin.
In Eclipse, create a new Java project, add a lib folder, and copy jacob.jar into it. Then create a package and a main class.
Configure the build path: right‑click the project → Build Path → Configure Build Path, then in the Libraries tab click Add JARs and select the JAR from the lib folder, apply and close.
Replace the generated main method with the following code, which creates COM objects for Sapi.SpVoice, sets volume and rate, speaks a string, then redirects the audio output to a file stream and saves a .wav file.
package com.badao.jacob;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class TTSMain {
public static void main(String[] args) {
ActiveXComponent ax = null;
String str = "Sample text for speech synthesis";
try {
ax = new ActiveXComponent("Sapi.SpVoice");
// Output speech content at runtime
Dispatch spVoice = ax.getObject();
// Volume 0-100
ax.setProperty("Volume", new Variant(100));
// Speech rate -10 to +10
ax.setProperty("Rate", new Variant(-2));
// Execute speech
Dispatch.call(spVoice, "Speak", new Variant(str));
// Below is building a file stream to generate an audio file
ax = new ActiveXComponent("Sapi.SpFileStream");
Dispatch spFileStream = ax.getObject();
ax = new ActiveXComponent("Sapi.SpAudioFormat");
Dispatch spAudioFormat = ax.getObject();
// Set audio stream format
Dispatch.put(spAudioFormat, "Type", new Variant(22));
// Set file output stream format
Dispatch.putRef(spFileStream, "Format", spAudioFormat);
// Call output file stream open method to create a .wav file
Dispatch.call(spFileStream, "Open", new Variant("D:\\badao.wav"), new Variant(3), new Variant(true));
// Set the voice object's audio output stream to the output file object
Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
// Set volume 0-100
Dispatch.put(spVoice, "Volume", new Variant(100));
// Set speech rate
Dispatch.put(spVoice, "Rate", new Variant(-2));
// Start speaking
Dispatch.call(spVoice, "Speak", new Variant(str));
// Close output file
Dispatch.call(spFileStream, "Close");
Dispatch.putRef(spVoice, "AudioOutputStream", null);
spAudioFormat.safeRelease();
spFileStream.safeRelease();
spVoice.safeRelease();
ax.safeRelease();
System.out.println("Speech synthesis succeeded");
} catch (Exception e) {
e.printStackTrace();
}
}
}Specify the output path (e.g., D:\badao.wav) and ensure the directory exists. Right‑click the class in Eclipse and choose Run As → Java Application. If no errors appear and the console prints the success message, the WAV file will be created at the specified location.
Example code can be downloaded from the provided link.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
