关闭

讯飞语音合成_3_java web实现

147人阅读 评论(0) 收藏 举报
分类:

从讯飞开放平台中下载语音合成的JAVA对应的SDK


新建java web项目

在WEB-INF中添加Jar包:

Msc.jar

json-jena-1.0.jar


其中msc32.dll 和msc64.dll是window32和window64的文件(这里我试过的两个可行的放的地方,1、C盘的System32文件夹下 2、Eclipse安装目录下,因为系统变量的Path里有添加过安装目录的路径了,3、将这两个文件单独放到一个目录下,然后将这个目录添加到系统变量的path下)



之后我实现的功能是:在JSP中添加一个表单,这个表单是语音合成需要的参数以及待合成的文本。然后将表单中的值传入Servlet中,然后在Servlet中读取表单中传入的参数的值,调用语音合成功能的类。


index.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<style type="text/css">
    	th,td {
    		text-align: left;
    	}
    	table{
       		border-collapse:collapse;
        	border:1px solid black;
    	}
    	td {
    		border:1px solid black;
    	}    
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%request.setCharacterEncoding("UTF-8");%>
<form action="servlet/Servlet" method="post" >
    <table>
    	<tbody>
    		<tr>
    			<th>发言人</th>
    		</tr>
    		<tr>
    			<td>
    				<label>小燕</label>
    				<input type="radio" value="xiaoyan"  name="VOICE_NAME" />
   					<label>小宇</label>
    				<input type="radio" value="xiaoyu"  name="VOICE_NAME" />
    				<label>小峰</label>
    				<input type="radio" value="xiaofeng"  name="VOICE_NAME" />
    				<label>小梅</label>
    				<input type="radio" value="xiaomei"  name="VOICE_NAME" />
    			</td>
    		</tr>
    		<tr>
    			<th>背景音乐:</th>
    		</tr>
    		<tr>
    			<td>
    				<select name="BACKGROUND_SOUND">
      					<option value="1" >开</option>
      					<option value="0" selected="selected">关</option>
    				</select>
    			</td>
    		</tr>
    		<tr>
    			<th>语速</th>
    		</tr>
    		<tr>
    			<td>
    				<input type="text" value=" " name="SPEED" />
    			</td>
    		</tr>
    		<tr>
    			<th>语调</th>
    		</tr>
    		<tr>
    			<td>
    				<input type="text" value=" " name="PITCH" />
    			</td>
    		</tr>
    		    		<tr>
    			<th>音量</th>
    		</tr>
    		<tr>
    			<td>
    				<input type="text" value=" " name="VOLUME" />
    			</td>
    		</tr>
    		<tr>
    			<th>播放文本</th>
    		</tr>
    		<tr>
    			<td>
    				<input type="text" value=" " name="mText" />
    			</td>
    		</tr>
    		<tr>
    			<td>
    				<input class="button" type="submit" value="提交">
    				<input class="button" type="reset" value="重置"  />
    			</td>
    		</tr>
    	</tbody>
    </table>   
</form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>XunFeiDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>servlet</servlet-name>
  	<servlet-class>servlet.Servlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>servlet</servlet-name>
  	<url-pattern>/servlet/Servlet</url-pattern>
  </servlet-mapping>
</web-app>

在servlet包下建立Servlet类

package servlet;

import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.Service;
import util.Util;




public class Servlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String voiceName=null;
		String backgoundSound=null;
		String speed=null;
		String pitch=null;
		String volume=null;
		String mText=null;
		if(req.getParameter("VOICE_NAME")!=null || req.getParameter("VOICE_NAME").trim().length()!=0){
			voiceName=req.getParameter("VOICE_NAME").trim();
		}
		if(req.getParameter("BACKGROUND_SOUND")!=null || req.getParameter("BACKGROUND_SOUND").trim().length()!=0){
			backgoundSound = req.getParameter("BACKGROUND_SOUND").trim();
		}
		if(req.getParameter("SPEED")!=null ||req.getParameter("SPEED").trim().length()!=0){
			speed =  req.getParameter("SPEED").trim();
		}
		if(req.getParameter("PITCH")!=null ||req.getParameter("PITCH").trim().length()!=0){
			pitch = req.getParameter("PITCH").trim();
		}
		if(req.getParameter("VOLUME")!=null ||req.getParameter("VOLUME").trim().length()!=0){
			volume = req.getParameter("VOLUME").trim();
		}
		if(req.getParameter("mText")!=null || req.getParameter("mText").trim().length()!=0){
			//mText = new String(req.getParameter("mText").getBytes("ISO8859-1"),"UTF-8");
			mText = req.getParameter("mText");
		}
//String path=req.getSession().getServletContext().getRealPath("/");//项目根路径
//D:\Eclipse\project\xunfei\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\XunFeiDemo\ 
		Map<String, String> mParamMap=Util.initiateParam(voiceName,backgoundSound,speed,pitch,volume);
//PrintWriter out = resp.getWriter();
//out.write(mParamMap.get("VOICE_NAME"));
//out.write(mParamMap.get("BACKGROUND_SOUND"));
//out.write(mParamMap.get("SPEED"));
//out.write(mParamMap.get("VOLUME"));
//out.write(mText);
		new Service().start(mParamMap,mText);
		
		//请求转发,服务器行为,转发后的对象仍存在
		//req.getRequestDispatcher("/index.jsp").forward(req, resp);
		
		//请求转发
		resp.sendRedirect("../index.jsp");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
		
	}



}

Servlet类中调用的工具类

package util;

import java.util.HashMap;
import java.util.Map;



public class Util {

	private static Map<String, String> mParamMap = new HashMap<String, String>();


	public static Map<String, String> initiateParam(String voiceName, String backgoundSound,
			String speed, String pitch, String volume) {
		mParamMap.put("VOICE_NAME", voiceName);
		mParamMap.put("BACKGROUND_SOUND", backgoundSound);
		mParamMap.put("SPEED", speed);
		mParamMap.put("PITCH", pitch);
		mParamMap.put("VOLUME", volume);
		return mParamMap;
	}

}

语音合成的功能类

package service;

import java.util.Map;


import com.iflytek.cloud.speech.*;

public class Service {
	
	public static SynthesizerListener synthesizeListener;

	public void start(Map<String, String> mParamMap,String mText) {
		// TODO Auto-generated method stub
		Setting.setShowLog(true);
		SpeechUtility.createUtility(SpeechConstant.APPID +"=57a0b95e");   
//System.out.println(mText);		
//System.out.println(mParamMap.get("VOICE_NAME"));
//System.out.println(mParamMap.get("BACKGROUND_SOUND"));
//System.out.println(mParamMap.get("SPEED"));
//System.out.println(mParamMap.get("PITCH"));
//System.out.println(mParamMap.get("VOLUME"));

//String path=this.getClass().getClassLoader().getResource("/").getPath(); 
//xxx/xunfei/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/XunFeiDemo/WEB-INF/classes/


		SpeechSynthesizer mTts= SpeechSynthesizer.createSynthesizer( );  
		
		mTts.setParameter(SpeechConstant.VOICE_NAME, mParamMap.get("VOICE_NAME"));
		mTts.setParameter(SpeechConstant.BACKGROUND_SOUND,mParamMap.get("BACKGROUND_SOUND"));
		mTts.setParameter(SpeechConstant.SPEED, mParamMap.get("SPEED")); 
		mTts.setParameter(SpeechConstant.PITCH, mParamMap.get("PITCH"));
		mTts.setParameter(SpeechConstant.VOLUME, mParamMap.get("VOLUME"));
//String path=mParamMap.get("ProjectPath").replace('\\', '/');
//System.out.println(path+"testService.pcm");
//mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH,  mParamMap.get("ProjectPath")+"testService.pcm");
		
		synthesizeListener = new SynthesizerListener() {

			@Override
			public void onBufferProgress(int arg0, int arg1, int arg2,
					String arg3) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onCompleted(SpeechError error) {
				// TODO Auto-generated method stub
				   System.out.println( "onCompleted enter: "+error );
				   if( null != error ){
				     System.out.println( "error: "+error.getErrorCode() );
				   }
				   System.out.println( "onCompleted leave" );
			}

			@Override
			public void onSpeakBegin() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSpeakPaused() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSpeakProgress(int arg0, int arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSpeakResumed() {
				// TODO Auto-generated method stub
				
			}  
		 
		}; 
		
		mTts.startSpeaking( mText, synthesizeListener );
	}


}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10809次
    • 积分:320
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条