⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 the speaktext.java file.txt

📁 Creating a Speech-Enabled Calendar Application
💻 TXT
字号:
/* Import required packages*/
import java.io.File;
import java.util.Locale;
import java.util.Vector;
import javax.speech.Central;
import javax.speech.Engine;
import javax.speech.EngineList;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.SynthesizerProperties;
import javax.speech.synthesis.Voice;
/*
Class: SpeakText-Converts the text value to speech.
Methods:
listAllVoices(): Retrieves all the voices that the end user's system supports
*/
public class SpeakText 
{
   /*
   listAllVoices():List all the system voices.
   Parameters:
   modeName:An object of String class.
   Return Type: NA
   */
   public static void listAllVoices(String modeName) 
   {
      System.out.println();
      /* 
      Print the mode name and locale for the voice synthesizer
      */
      System.out.println(
      "All " + modeName + " Mode JSAPI Synthesizers and Voices:");
      SynthesizerModeDesc required = new SynthesizerModeDesc(
      null,
      modeName,
      Locale.US,
      null,
      null);
      /*
      Retrieve all the available synthesizers 
      */
      EngineList engineList = Central.availableSynthesizers(required);
      /*
      Start a loop from 0 to the total number of available synthesizers
      */
      for (int i = 0; i < engineList.size(); i++) 
      {
         SynthesizerModeDesc desc = (SynthesizerModeDesc) engineList.get(i);
         /*
         Print the name, mode, and locale
         */
         System.out.println("    " + desc.getEngineName()
         + " (mode=" + desc.getModeName()
         + ", locale=" + desc.getLocale() + "):");
         /*
         Retrieve all the available voices in an array
         */
         Voice[] voices = desc.getVoices();
         /*
         Print all the available voices on console
         */
         for (int j = 0; j < voices.length; j++) 
         {
            System.out.println("        " + voices[j].getName());
         }
      }
   }
   /*      
   SpeakText(): Default constructor of SpeakText class
   Parameter:
   k: An object of String class.
   Return Type:NA
   */
   public SpeakText(String k) 
   {
      String l=k;
      /*
      List all the available voices by calling the ListAllVoices() method.
      */
      listAllVoices("general");
      /*
      Set the voice to kevin16
      */
      String voiceName = "kevin16";
      System.out.println();
      System.out.println("Using voice: " + voiceName);
      try 
      {
         SynthesizerModeDesc desc = new SynthesizerModeDesc
         (
            null,          
            "general",     
            Locale.US,     
            null,          
            null);         
            Synthesizer synthesizer = Central.createSynthesizer(desc);
            /*
            Check if the value of synthesizer is null
            */
            if (synthesizer == null) 
            {
               /*
               Print the error message
               */
               String message = "\nCan't find synthesizer.\n"
               + "Make sure that there is a \"speech.properties\" file "
               + "at either of these locations: \n";
               message += "user.home    : "
               + System.getProperty("user.home") + "\n";
               message += "java.home/lib: " + System.getProperty("java.home")
               + File.separator + "lib\n";
               System.err.println(message);
               System.exit(1);
             }
            /*
            Allocate the synthesizer
            */
            synthesizer.allocate();
            synthesizer.resume();
            /*
            Retrieve the engine mode description of the synthesizer
            */
            desc = (SynthesizerModeDesc) synthesizer.getEngineModeDesc();
            /*
            Retrieve the voices for the synthesizer
            */
            Voice[] voices = desc.getVoices();
            Voice voice = null;
            for (int i = 0; i < voices.length; i++) 
            {
               if (voices[i].getName().equals(voiceName)) 
               {
                  voice = voices[i];
                  break;
               }
            }
            /*
            Checks if no voice exists
            */
            if (voice == null) 
            {
               /*
               Print a message that the voice selected does not exists
               */
               System.err.println("Synthesizer does not have a voice named "+ voiceName + ".");
               System.exit(1);
            }
            /*
            Set the voice for the synthesizer
            */
            synthesizer.getSynthesizerProperties().setVoice(voice);
            /*
            Speak the text using the speakPlainText() method
            */
            synthesizer.speakPlainText(l, null);
            synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
         } 
         catch (Exception e) 
         {
            e.printStackTrace();
         }
      }
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -