프론트엔드 공부/js예제
텍스트를 음성으로 변환 가능한 코드
maggieH
2021. 5. 27. 22:52
function speak(text, opt_prop) {
if (typeof SpeechSynthesisUtterance === "undefined" || typeof window.speechSynthesis === "undefined") {
alert("이 브라우저는 음성 합성을 지원하지 않습니다.")
return
}
window.speechSynthesis.cancel() // 현재 읽고있다면 초기화
const prop = opt_prop || {}
const speechMsg = new SpeechSynthesisUtterance()
speechMsg.rate = prop.rate || 1 // 속도: 0.1 ~ 10
speechMsg.pitch = prop.pitch || 1 // 음높이: 0 ~ 2
speechMsg.lang = prop.lang || "ko-KR"
speechMsg.text = text
// SpeechSynthesisUtterance에 저장된 내용을 바탕으로 음성합성 실행
window.speechSynthesis.speak(speechMsg)
}
})
https://codepen.io/fangmin/pen/GRWvMpr