通過調用百度接口實現
接口地址:http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text=你要轉換的文字
參數說明:
lan=zh:語言是中文,如果改爲lan=en,則語言是英文。
ie=UTF-8:文字格式。
spd=2:語速,可以是1-9的數字,數字越大,語速越快。
text=**:這個就是你要轉換的文字
js代碼示例:
function audioPlay(text){ var zhText = text; zhText = encodeURI(zhText); var audio = "<audio autoplay=\"autoplay\">" + "<source src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=4&text=" + zhText + "\" type=\"audio/mpeg\">" + "<embed height=\"0\" width=\"0\" src=\"http://tts.baidu.com/text2audio?text=" + zhText + "\">" + "</audio>"; $('body').append(audio); }
//在需要的地方調用該方法
audioPlay("Hello World");