Files
vue-pult/server/wmp-radio-player.js
sasha 3e90269b0b Initial commit: Vue.js тир управления система
- Клиентская часть Vue 3 + Vite
- Серверная часть Node.js + WebSocket
- Система авторизации и смен
- Управление игровыми портами
- Поддержка тем (светлая/темная)
- Адаптивный дизайн

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-19 12:24:22 +03:00

110 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Радио плеер через Windows Media Player COM объект
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
class WMPRadioPlayer {
constructor() {
this.radioProcess = null;
this.isPlaying = false;
this.volume = 70;
this.currentUrl = null;
}
play(url) {
this.stop();
// Добавляем протокол если его нет
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = 'http://' + url;
}
console.log('[RADIO] 📻 Запуск радио через WMP:', url);
this.currentUrl = url;
// VBScript для управления Windows Media Player
const vbscript = `
Set WMP = CreateObject("WMPlayer.OCX")
WMP.settings.autoStart = True
WMP.settings.volume = ${this.volume}
WMP.URL = "${url}"
' Ждем пока не начнет играть
Do While WMP.playState <> 3
WScript.Sleep 100
Loop
WScript.Echo "Playing"
' Держим скрипт активным
Do While True
WScript.Sleep 1000
If WMP.playState <> 3 Then
WScript.Echo "Stopped"
WScript.Quit
End If
Loop`;
// Сохраняем скрипт
const scriptPath = path.join(__dirname, 'temp_radio.vbs');
fs.writeFileSync(scriptPath, vbscript);
// Запускаем через cscript (консольный режим)
this.radioProcess = spawn('cscript', ['//NoLogo', scriptPath]);
this.radioProcess.stdout.on('data', (data) => {
const output = data.toString().trim();
console.log('[RADIO] Статус:', output);
if (output === 'Playing') {
this.isPlaying = true;
} else if (output === 'Stopped') {
this.isPlaying = false;
}
});
this.radioProcess.stderr.on('data', (data) => {
console.error('[RADIO] Ошибка:', data.toString());
});
this.radioProcess.on('exit', (code) => {
console.log('[RADIO] Процесс завершен с кодом:', code);
this.isPlaying = false;
// Удаляем временный файл
try {
fs.unlinkSync(scriptPath);
} catch (e) {}
});
this.radioProcess.on('error', (err) => {
console.error('[RADIO] Ошибка запуска:', err.message);
this.isPlaying = false;
});
}
stop() {
if (this.radioProcess) {
console.log('[RADIO] ⏹️ Остановка радио');
this.radioProcess.kill();
this.radioProcess = null;
// Также убиваем все WMP процессы
spawn('taskkill', ['/F', '/IM', 'wmplayer.exe'], { stdio: 'ignore' });
}
this.isPlaying = false;
this.currentUrl = null;
}
setVolume(volume) {
this.volume = Math.max(0, Math.min(100, volume));
console.log('[RADIO] 🔊 Установка громкости:', this.volume);
// Для изменения громкости нужно перезапустить
if (this.isPlaying && this.currentUrl) {
const url = this.currentUrl;
this.stop();
setTimeout(() => this.play(url), 500);
}
}
}
module.exports = WMPRadioPlayer;