[编辑:我自己 FITFO,大声笑 - 请参阅我在这里写的回复]
好的,所以我已经为此疯狂了好几个星期……</p>
我的 Drupal 站点上有多个 Wavesurfer 播放器实例。当我单击一个“播放”时,我希望任何其他当前正在播放的 Wavesurfer 实例暂停。
我已经尝试了我能找到的一切,但我无法让它工作。
这是 Drupal 实现 Wavesurfer 的方式:
(function ($, Drupal) {
'use strict';
Drupal.AudiofieldWavesurfer = {};
Drupal.AudiofieldWavesurfer.generate = function (context, file, settings) {
$.each($(context).find('#' + file.id).once('generate-waveform'), function (index, wavecontainer) {
var wavesurfer = WaveSurfer.create({
container: '#' + $(wavecontainer).attr('id') + ' .waveform',
audioRate: settings.audioRate,
autoCenter: settings.autoCenter,
barGap: settings.barGap,
barHeight: settings.barHeight,
barWidth: settings.barWidth,
cursorColor: settings.cursorColor,
cursorWidth: settings.cursorWidth,
forceDecode: settings.forceDecode,
normalize: settings.normalize,
progressColor: settings.progressColor,
responsive: settings.responsive,
waveColor: settings.waveColor
});
wavesurfer.load(file.path);
wavesurfer.setVolume(1);
$(wavecontainer).find('.player-button.playpause').on('click', function (event) {
Drupal.AudiofieldWavesurfer.PlayPause(wavecontainer, wavesurfer);
});
$(wavecontainer).find('.volume').on('change', function (event) {
wavesurfer.setVolume($(event.currentTarget).val() / 10);
});
if (!!settings.autoplay) {
wavesurfer.on('ready', wavesurfer.play.bind(wavesurfer));
}
});
};
Drupal.AudiofieldWavesurfer.PlayPause = function (wavecontainer, wavesurfer) {
wavesurfer.playPause();
var button = $(wavecontainer).find('.player-button.playpause');
if (wavesurfer.isPlaying()) {
$(wavecontainer).addClass('playing');
button.html('<i class="fas fa-pause-circle fa-2x"></i>');
} else {
$(wavecontainer).removeClass('playing');
button.html('<i class="fas fa-play-circle fa-2x"></i>');
}
};
这就是我目前试图暂停所有其他人的方式:
document.addEventListener('playPause', function(e){
var audios = document.getElementsByClassName('audiofield-wavesurfer');
for(var i = 0, len = audios.length; i < len; i++){
if(audios[i] != e.target && audios[i].hasClass('playing')){
audios[i].playPause();
}
}
}, true);
看来这行不通(一直在阅读 Drupal.behaviours 并尝试修改该部分,也没有运气。
我也尝试过让 PlayPause 函数在单击时通过每个播放器运行,如下所示:
Drupal.AudiofieldWavesurfer.PlayPause = function (wavecontainer, wavesurfer) {
$.each($(Drupal.AudiofieldWavesurfer), function() {
wavesurfer.playPause();
var button = $(wavecontainer).find('.player-button.playpause');
if (wavesurfer.isPlaying()) {
$(wavecontainer).addClass('playing');
button.html('<i class="fas fa-pause-circle fa-2x"></i>');
} else {
$(wavecontainer).removeClass('playing');
button.html('<i class="fas fa-play-circle fa-2x"></i>');
}
})};
……或在这里:
Drupal.AudiofieldWavesurfer.generate = function (context, file, settings) {
$.each($(context).find('#' + file.id).once('generate-waveform'), function (index, wavecontainer) {
var wavesurfer = WaveSurfer.create({
container: '#' + $(wavecontainer).attr('id') + ' .waveform',
audioRate: settings.audioRate,
autoCenter: settings.autoCenter,
barGap: settings.barGap,
barHeight: settings.barHeight,
barWidth: settings.barWidth,
cursorColor: settings.cursorColor,
cursorWidth: settings.cursorWidth,
forceDecode: settings.forceDecode,
normalize: settings.normalize,
progressColor: settings.progressColor,
responsive: settings.responsive,
waveColor: settings.waveColor
});
wavesurfer.load(file.path);
wavesurfer.setVolume(1);
// ---- HERE MY CODE: --------
$(wavecontainer).find('.player-button.playpause').on('click', function (event) {
$.each($(context).find('#' + file.id), function(idx, obj) {
$(obj).stop();
});
Drupal.AudiofieldWavesurfer.PlayPause($(wavecontainer), wavesurfer);
});
但所有这些对我都不起作用……有人知道如何帮助我解决这个问题吗?会救我的命!!!