简体   繁体   English

多个 wavesurfer.js 播放器

[英]Multiple wavesurfer.js players

When I have more than 1 instance on a single page the player doesn't work properly, I figured I'd have to create a foreach cicle of some sorts, if anyone could help me with this issue it would be much appreciated.当我在单个页面上有多个实例时,播放器无法正常工作,我想我必须创建某种 foreach cicle,如果有人可以帮助我解决这个问题,我将不胜感激。

Here's what I have:这是我所拥有的:

<script src='https://unpkg.com/wavesurfer.js'></script>
<script>
  duration = document.querySelector('#duration');
current = document.querySelector('#current');
playPause = document.querySelector('#playPause');
var timeCalculator = function (value) {
    second = Math.floor(value % 60);
    minute = Math.floor((value / 60) % 60);
    if (second < 10) {
        second = '0' + second;
    }
    return minute + ':' + second;
};

wavesurfer = WaveSurfer.create({
    container: '#wave',
    waveColor: '#a43152',
    progressColor: '#5b1b2d',
    height: 30,
    barRadius: 2,
    barWidth:3,
    barGap:2,
    scrollParent: false
});

wavesurfer.load('files/$title.mp3');

playPause.addEventListener('click', function (e) {
    wavesurfer.playPause();
});

wavesurfer.on('ready', function (e) {
    duration.textContent = timeCalculator(wavesurfer.getDuration());
});

wavesurfer.on('audioprocess', function (e) {
    current.textContent = timeCalculator(wavesurfer.getCurrentTime());
});

wavesurfer.on('play', function (e) {
    playPause.classList.remove('fi-rr-play');
    playPause.classList.add('fi-rr-pause');
});

//change pause button to play on pause
wavesurfer.on('pause', function (e) {
    playPause.classList.add('fi-rr-play');
    playPause.classList.remove('fi-rr-pause');
});

wavesurfer.on('seek', function (e) {
    current.textContent = timeCalculator(wavesurfer.getCurrentTime());
});
</script>";
        
    

You need to create multiple wavesurfer instances with different IDs.您需要创建多个具有不同 ID 的 wavesurfer 实例。 So you can create each one by using a for loop with因此,您可以使用 for 循环来创建每一个

container: '#wave'+index容器:'#wave'+索引

and inside your for loop you will also need to add listeners for play/pause/seek...在你的 for 循环中,你还需要为播放/暂停/搜索添加侦听器...

The only issue will be loading all mp3s, if you are loading 2-5 smaller files you should be fine, but loading more can slow down your page performance.唯一的问题是加载所有 mp3,如果您加载 2-5 个较小的文件应该没问题,但加载更多会降低页面性能。 I recommend using drawBuffer() and then loading mp3s when the user clicks play.我建议使用 drawBuffer(),然后在用户单击播放时加载 mp3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM