百度文字轉語音免費接口使用實例

一、接口javascript

http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text=你要轉換的文字    上述接口的url,在瀏覽器上直接打開,便可聽到文字轉換後的語音。

lan=zh:語言是中文,若是改成lan=en,則語言是英文。html

ie=UTF-8:文字格式。java

spd=2:語速,能夠是1-9的數字,數字越大,語速越快。瀏覽器

text=**:這個就是你要轉換的文字。post

二、js調用url

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<form action="" method="post">
		<table align="center">
			<tr>
				<td><input type="text" id='val' placeholder='你要裝換的文字'></td>
				<td><input type="button" value="提交" onclick="fun()"></td>
			</tr>
		</table>
	</form>
</body>
</html>
<script type="text/javascript">
function fun()
{
	var val=document.getElementById("val").value;
	var zhText = val;
	zhText = encodeURI(zhText);
	document.write("<audio autoplay=\"autoplay\">");
	document.write("<source src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text="+ zhText +"\" type=\"audio/mpeg\">");
	document.write("<embed height=\"0\" width=\"0\" src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text="+ zhText +"\">");
	document.write("</audio>");
}
</script>