Free javascript Hosting


kk-url.js

Uploaded on Apr 11 2022 18:16 by uioiu9420

//=====================================;
/* kk('kaz su') 烏衣行連續音檔播放器;
<a onclick="kk('kaz su')">漢字</a>
*/
//var kkUpUrl = "https://twblg.dict.edu.tw/holodict_new/audio/"; //
//可以在網頁自己指定預設音檔網址;
let kkUpUrl;

function kk(y, rate = 1, count = 1) {
// kk(音檔, 速度, 播放次數)
let x = document.getElementById("WeSingAudioKK");
if(!x){
let audio = document.createElement("div");
audio.innerHTML = `<audio id="WeSingAudioKK"></audio>`
document.getElementsByTagName("body")[0].appendChild(audio);
}
x = document.getElementById("WeSingAudioKK");

if (!kkUpUrl){
kkUpUrl = "";
}else if (kkUpUrl != "" && kkUpUrl.slice(-1, ) != "/") {
kkUpUrl = kkUpUrl + "/";
}// 上層網址最右邊忘了/,則補上;

y = y.replace(/\.mp3/g, ""); //移除.mp3,之後再加;
y = y.trim().replace(/\s+/g, " "); // 移除多於空格;標音間空格改為空格,給後面切割;
let arr = y.split(" ");
let len = arr.length;

let i = 0; // 第i音檔;
let n = 0; // 播放第n次;

myPlay();

function myPlay() {
mp3Url(arr[i]);
x.play();
x.playbackRate = rate;
x.onended = () => playStop();
x.onerror = () => playStop();
}
function playStop() {
if (i == len - 1 && n == count-1) {
x.currentTime = 0
x.pause();
}else if (i == len - 1 || arr[i] == null) {
i = 0;
myPlay();
n += 1;
} else {
i = i + 1;
myPlay();
}
}
function mp3Url(url) {
if (url.match("https://drive.google")) {
let re = /(\/d\/)(.*)(\/view)/gi;
let txt = "https://docs.google.com/uc?export=download&id=" + url.match(re);
txt = txt.replace(/(\/d\/)(.*)(\/view)/gi, "$2");
// 若用Google雲端硬碟直接分享網址,被轉成可下載且播放網址;
x.src = txt;
} else if (url.match("https://docs.google")) {
x.src = url;
// 若用Google雲端硬碟可下載且播放網址;
} else if (url.match("https://api.voicerss.org")) {
x.src = url;
// 若用api.voicerss.org,直接播放網址;
} else if (url.match("http")) {
// 若是以 http 開頭,則直接以其網址為網址,並加回.mp3;
x.src = url + ".mp3";
} else {
x.src = kkUpUrl + url + ".mp3";
// 否則,以上網址 + 文字 + .mp3;
}
return;
}
}

Back to list