java实现文本读音

1方法--js实现:


    function kk(){
    	  var VoiceObj = null;
		try {
			VoiceObj = new ActiveXObject("SAPI.SpVoice");
		} catch (e) {
			alert(e);
		}
		}
		//停止播报
		function stopReadText() {
			VoiceObj.Speak("", 2);
		}
		//播报文字
		function readTextOne(p_str) {
			if (p_str != null && p_str != "") {
				try {
					//开始播报
					VoiceObj.Rate = -4;
					VoiceObj.Speak(p_str);
					VoiceObj.Speak("", 2);//停止播报
				} catch (exception) {
					alert("error:" + exception);
				}
			}
		}
		//语速加
		function IncRate() {
			if (VoiceObj.Rate < 10) {
				VoiceObj.Rate = VoiceObj.Rate + 1;
			}
		}

		//语速减
		function DecRate() {
			if (VoiceObj.Rate > -10) {
				VoiceObj.Rate = VoiceObj.Rate - 1;
			}
		}
  //调用这个,或者你爱咋弄咋弄
		function setMessage(){
			var txt = '请哈哈到666就诊';
			window.setTimeout("speakTxt('"+txt+"')",1000);		
		}
		function speakTxt(txt){
			readTextOne(txt);
			readTextOne(txt);
			readTextOne(txt);
		}
    

2方法--后台实现:

下载jaob-1.17包,将里面的jar放到项目 下,根据电脑位数将.dll文件放到你的jdk/bin下,运行即可

ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
		Dispatch sapo = sap.getObject();
		try {

		    // 音量 0-100
		    sap.setProperty("Volume", new Variant(100));
		    // 语音朗读速度 -10 到 +10
		    sap.setProperty("Rate", new Variant(-1));

		    Variant defalutVoice = sap.getProperty("Voice");

		    Dispatch dispdefaultVoice = defalutVoice.toDispatch();
		    Variant allVoices = Dispatch.call(sapo, "GetVoices");
		    Dispatch dispVoices = allVoices.toDispatch();

		    Dispatch setvoice = Dispatch.call(dispVoices, "Item", new Variant(1)).toDispatch();
		    ActiveXComponent voiceActivex = new ActiveXComponent(dispdefaultVoice);
		    ActiveXComponent setvoiceActivex = new ActiveXComponent(setvoice);

		    Variant item = Dispatch.call(setvoiceActivex, "GetDescription");
		    // 执行朗读
		    Dispatch.call(sapo, "Speak", new Variant("请张少少到"));

		} catch (Exception e) {
		    e.printStackTrace();
		} finally {
		    sapo.safeRelease();
		    sap.safeRelease();
		}

 

你可能感兴趣的:(语音)