편집 : 지금은 사파리에서 작동합니다. 오디오는 사용자 이벤트 (예 : onclick ..)에 의해 트리거되어야하므로 버튼을 추가하고 타자기를 클릭하여 시작하도록했습니다.
추 신 : 오디오 파일을 미리로드 할 수있는 방법이 없다는 사파리는 서버 요청을하고 재생할 때마다 오디오를 다운로드합니다.
// The delay between each keystroke
var delay = 300;
// The typewriter sound file url
var clickURL = "https://cdn.rawgit.com/halimb/res/6ffa798d/typewriter.wav";
// Get a reference to the container div
var container = document.getElementById("container");
var sampleString = "Hello world!";
//get a reference to the start button and typewrite onclick
var start = document.getElementById("btn");
start.onclick = function() { typewrite(sampleString); };
function typewrite(str) {
var i = 0;
container.innerHTML = "";
type();
function type() {
var click = new Audio(clickURL);
// This is triggered when the browser has enough of the file to play through
click.oncanplaythrough = function() {
click.play();
// Add the character to the container div
container.innerHTML += str[i];
i++;
if(i < str.length) {
window.setTimeout(type, delay);
}
}
}
}
* {
\t font-family: Courier;
\t font-size: 32px;
}
.btn {
\t display: inline-block;
\t border: 1px solid black;
\t border-radius: 5px;
\t padding: 10px;
\t cursor: pointer;
\t margin: 10px;
}
<div class="btn" id="btn">Start</div>
<div id="container"></div>
을 : 나는 이것을 극복하는 생각할 수있는 유일한 (더러운) 방법은 재생 속도가 정말 중요한 경우 (
https://www.ibm.com/developerworks/library/wa-ioshtml5/를이 도움이 될 수있는) 것을 시도 할 수 있습니다 .. 데이터 대신 audioURL의 URI를 제공하는 것입니다
당신이하려고하는 것에 대해 조금 생각해보십시오. 문제를 해결하고, 각 부분을 인터넷으로 검색하고, 구현하고, 문제가 있다면 여기에서 그들에 대해 물어보십시오! 그런 광범위한 질문을하는 최고의 장소가 아닙니다. – Kroltan