/
memoryspeak
/
MemorySpeakWebClient
Обзор
Документация
Войти
/
memoryspeak
/
MemorySpeakWebClient
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
static/js/AudioPlayer.js
77 строк
3 KB
memoryspeak
initial commit: project structure
20 ноя 2025, 21:54
20 ноя 2025, 21:54
9ddd280
Код
Авторство
О чём код?
class AudioPlayer { static list = []; // Список аудиоэлементов static currentTrackIndex = -1; // Текущий играющий трек static isShuffleMode = false; // Режим случайного воспроизведения // Добавление трека в плейлист static addTrack(audioElement) { this.list.push(audioElement); audioElement.addEventListener('play', () => { this.stopAllExcept(audioElement); this.currentTrackIndex = this.list.indexOf(audioElement); }); audioElement.addEventListener('ended', () => { if (this.isShuffleMode) { this.playRandom(); } else { this.playNext(); }; }); }; // Остановка всех треков кроме указанного static stopAllExcept(currentAudio) { this.list.forEach(audio => { if (audio !== currentAudio && !audio.paused) { audio.pause(); audio.currentTime = 0.0; }; }); }; // Воспроизведение следующего трека static playNext() { if (this.list.length === 0) return; this.currentTrackIndex = (this.currentTrackIndex + 1) % this.list.length; this.playAudio(); }; // Воспроизведение предыдущего трека static playPrevious() { if (this.list.length === 0) return; this.currentTrackIndex = (this.currentTrackIndex - 1 + this.list.length) % this.list.length; this.playAudio(); }; // Воспроизведение случайного трека static playRandom() { if (this.list.length === 0) return; let randomIndex; do { randomIndex = Math.floor(Math.random() * this.list.length); } while (randomIndex === this.currentTrackIndex && this.list.length > 1); this.currentTrackIndex = randomIndex; this.playAudio(); }; // Переключение режима случайного воспроизведения static toggleShuffle() { this.isShuffleMode = !this.isShuffleMode; return this.isShuffleMode; }; static playAudio() { document.getElementById(`search_file_hr_${this.list[this.currentTrackIndex].id.split('_')[5]}_${this.list[this.currentTrackIndex].id.split('_')[6]}_${this.list[this.currentTrackIndex].id.split('_')[7]}`).scrollIntoView({behavior: "smooth"}); if (this.list[this.currentTrackIndex].parentNode.parentNode.style.display === "none") document.getElementById(`search_card_file_audio_${this.list[this.currentTrackIndex].id.split('_')[5]}_${this.list[this.currentTrackIndex].id.split('_')[6]}_${this.list[this.currentTrackIndex].id.split('_')[7]}`).click(); this.list[this.currentTrackIndex].play(); }; };