forked from cclient/chrome-extensions-youdaowithwordnode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.js
68 lines (59 loc) · 1.51 KB
/
speech.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var _audioStartTime = 0;
var _autoplay = false;
var _audioPlayer = function () {
var audio = document.createElement('audio');
audio.setAttribute('controls', 'controls');
audio.style.display = 'none';
if (_autoplay)
audio.setAttribute('autoplay', 'autoplay');
// audio.setAttribute('src', src);
document.body.appendChild(audio);
function whenAudioPlay(){
var _t = +new Date - _audioStartTime;
// audio.removeEventListener("playing", whenAudioPlay, false);
// console.log('audio playing, ', _t)
_rlog.push(["_trackCustom", "actions", [
['audio_play_click_to_playing', _t],
['audio_play_src', this.getAttribute('src')]
]]);
}
audio.addEventListener("playing", whenAudioPlay, false);
return audio;
};
var _audio = _audioPlayer();
function _playSound (url) {
_stopSound();
_audioStartTime = +new Date;
_audio.setAttribute('src', url);
_audio.play();
}
function _stopSound() {
if (_audio.pause) {
_audio.pause();
if (_audio.currentTime > 0) {
_audio.currentTime = 0;
}
}
}
function _setAutoPlay(autoplay) {
_autoplay = autoplay;console.log(_audio)
if (_audio) {
if (autoplay)
_audio.setAttribute('autoplay', 'autoplay');
else
_audio.removeAttribute('autoplay');
}
}
var playAudio = function () {
var url = arguments[0];
// stopVideo();
// external.playSound(url);
_playSound(url);
};
var stopAudio = function () {
// external.stopSound();
_stopSound();
};
var setAutoPlay = function (autoplay) {
_setAutoPlay(autoplay);
};