/*******朗读开始********/ (function() { var changeState = function(o, old, n) { o.setAttribute("data-state", n); var clazz = o.getAttribute("class"); if (clazz && clazz.length) { clazz = clazz.indexOf(old) >= 0 ? clazz.replace(new RegExp(old, "gi"), n) : clazz + " " + n; } else { clazz = n; } o.setAttribute("class", clazz); }; function newAudio(src, onended) { var audio = document.createElement("audio"); audio.src = src; audio.preload = "auto"; audio.onended = onended; audio.loop = false; return audio; } function V() { this.token = null; this.index = 0; this.playing = false; this.onend = null; this.srcs = []; this.audio = null; this.audio2 = null; } V.prototype = { start: function() { this.index = 0; this.playing = true; if (!this.audio) { this.audio = newAudio(this.srcs[0], this.next.bind(this)); this.audio.load(); } else { this.audio.src = this.srcs[0]; this.audio.load(); } if (this.srcs.length > 1) { if (!this.audio2) { this.audio2 = newAudio(this.srcs[1], this.next.bind(this)); this.audio2.load(); } else { this.audio2.src = this.srcs[1]; this.audio2.load(); } } this.audio.play(); }, resume: function() { if (this.playing) { this.audio.play(); } else { this.start(); } }, pause: function() { if (this.playing) { this.audio.pause(); } }, next: function() { this.index++; while (this.index < this.srcs.length && !this.srcs[this.index] ) { // skip deleted this.index++; } if (this.index >= this.srcs.length) { this.playing = false; if (typeof this.onend === "function") this.onend(); return; } var tmp = this.audio; this.audio = this.audio2; this.audio2 = tmp; if (this.srcs.length > this.index + 1) { this.audio2.src = this.srcs[this.index+1]; this.audio2.load(); } if (this.audio.duration > 0) { this.audio.play(); } else { this.audio.src = ""; this.audio.load(); this.srcs[this.index] = undefined; this.next(); } }, onEnd: function(callback) { this.onend = callback; }, init: function(text, callback) { var self = this; self.getToken(function(token) { self.index = 0; self.playing = false; var texts = text.match(/.{0,1000}/g); texts.forEach(function(t) { if (!t.length) return; self.srcs[self.srcs.length] = "http://tsn.baidu.com/text2audio?tok=" + token + "&lan=zh&cuid=test&ctp=1&spd=5&pit=5&vol=8&per=0&tex=" + encodeURIComponent(t); }); self.start(); self.audio.onloadeddata = function() { callback(); self.audio.onloadeddata = undefined; } }); return self; }, getToken: function (callback) { var self = this; if (self.token) callback(self.token); else { var req = new XMLHttpRequest(); req.addEventListener('load', function() { self.token = req.responseText; callback(self.token); }); req.open("GET", "http://hanwen360.com/v"); req.send(); } }, play: function(text) { var self = this; self.init(text, function() { self.start(); }); }, ui: function(text) { var v = this; var con = document.createElement("div"); con.setAttribute("id", "vui-con"); var c = document.createElement("div"); con.appendChild(c); c.setAttribute("id", "vui"); // var state = "init"; c.setAttribute("data-state", "init"); c.addEventListener("click", function() { var old = c.getAttribute("data-state"); var state; if (old === "init") { state = "loading"; changeState(c, "init", "loading"); v.init(text, function() { changeState(c, "loading", "playing"); }).onEnd(function() { changeState(c, "playing", "stopped"); }); } else if (old === "playing") { state = "paused"; v.pause(); changeState(c, old, state); } else if (old === "paused") { state = "playing"; v.resume(); changeState(c, old, state); } else if (old === "stopped") { state = "playing"; v.start(); changeState(c, old, state); } }); return con; } }; window.V = V; })(); $(document).ready(function() { var postTitle = $(".post-title"); if (!postTitle.length) return; var text = postTitle.text() + "。" + $(".spider").text(); if (!text.length) return; text = text.replace(/[ —]*/g, "").replace(/\n+/g, "。"); var v = new V(); postTitle.append(v.ui(text)); }); /*******朗读结束********/