第36行: |
第36行: |
| | | |
| /* 生成音频播放按钮 */ | | /* 生成音频播放按钮 */ |
− | (window.onload = function (){ | + | (function (){ |
| var audios = document.getElementsByClassName('audio-player'); | | var audios = document.getElementsByClassName('audio-player'); |
| var notValidAudio = /wpDestFile/; | | var notValidAudio = /wpDestFile/; |
第61行: |
第61行: |
| | | |
| a.src = link; | | a.src = link; |
− | a.preload = 'none'; | + | a.preload = 'auto'; |
| | | |
| a.onended = function () { | | a.onended = function () { |
第68行: |
第68行: |
| }; | | }; |
| | | |
− | var button = document.createElement('button'); | + | a.addEventListener('canplaythrough', function() { |
| + | var button = document.createElement('button'); |
| | | |
− | button.classList.add('audio-player-button');
| + | button.classList.add('audio-player-button'); |
− | button.append(a);
| + | button.append(a); |
− | button.onclick = function () {
| + | button.onclick = function() { |
− | var a = this.firstElementChild;
| + | if (currentPlaying != null && currentPlaying != a) { |
| + | currentPlaying.pause(); |
| + | currentPlaying.currentTime = 0; |
| + | currentPlaying.parentNode.classList.remove('audio-player-playing'); |
| + | } |
| | | |
− | if (currentPlaying != null && currentPlaying != a) {
| + | if (a.paused) { |
− | currentPlaying.pause();
| + | a.play(); |
− | currentPlaying.currentTime = 0;
| + | currentPlaying = a; |
− | currentPlaying.parentNode.classList.remove('audio-player-playing');
| + | this.classList.add('audio-player-playing'); |
− | } | + | this.classList.remove('audio-player-paused'); |
| + | } else { |
| + | a.pause(); |
| + | this.classList.add('audio-player-paused'); |
| + | this.classList.remove('audio-player-playing'); |
| + | } |
| + | }; |
| + | |
| + | audio.append(button); |
| + | }); |
| | | |
− | if (a.paused) {
| + | audio.append(a); |
− | a.play();
| + | } |
− | currentPlaying = a;
| |
− | this.classList.add('audio-player-playing');
| |
− | this.classList.remove('audio-player-paused');
| |
− | }
| |
− | else {
| |
− | a.pause();
| |
− | this.classList.add('audio-player-paused');
| |
− | this.classList.remove('audio-player-playing');
| |
− | };
| |
− | };
| |
− |
| |
− | audio.append(button); | |
− | }; | |
| })(); | | })(); |
| | | |