mpg123
This commit is contained in:
parent
21f18d1b2a
commit
f236665829
@ -43,9 +43,9 @@
|
||||
<button id="forward">10s +</button>
|
||||
</div>
|
||||
|
||||
<audio id="audio"
|
||||
<!-- <audio id="audio"
|
||||
src="muddy_files.mp3"
|
||||
data-reverse-src="muddy_files_reverse.mp3"></audio>
|
||||
data-reverse-src="muddy_files_reverse.mp3"></audio> -->
|
||||
|
||||
</main>
|
||||
|
||||
|
||||
@ -19,7 +19,8 @@ const rotationsPerTrack = 3; // tours complets du vinyle sur toute la durée
|
||||
let currentRotation = 0; // rotation en degrés actuelle du vinyle
|
||||
|
||||
// 3. Connexion Socket.io
|
||||
const socket = io();
|
||||
|
||||
/* const socket = io();
|
||||
|
||||
socket.on('position', (position) => {
|
||||
const now = Date.now();
|
||||
@ -147,4 +148,65 @@ audio.addEventListener('ended', () => {
|
||||
} else {
|
||||
isPlaying = false;
|
||||
}
|
||||
}); */
|
||||
|
||||
const socket = io();
|
||||
|
||||
let isPlaying = false;
|
||||
|
||||
const rotationsPerTrack = 3;
|
||||
let currentRotation = 0;
|
||||
|
||||
// Valeurs fictives pour durée, car tu ne peux plus récupérer la durée côté client
|
||||
const DURATION = 96; // durée en secondes (1:36)
|
||||
|
||||
// Simulation simple du temps qui passe côté client pour la rotation du vinyle
|
||||
let currentTime = 0;
|
||||
let animationFrameId;
|
||||
|
||||
function playTrack() {
|
||||
socket.emit('play-sound', 'muddy_files.mp3');
|
||||
isPlaying = true;
|
||||
startVinylRotation();
|
||||
}
|
||||
|
||||
function stopTrack() {
|
||||
socket.emit('stop-sound');
|
||||
isPlaying = false;
|
||||
stopVinylRotation();
|
||||
currentTime = 0;
|
||||
updateVinylRotation();
|
||||
}
|
||||
|
||||
function startVinylRotation() {
|
||||
function update() {
|
||||
if (isPlaying) {
|
||||
currentTime += 0.1; // avance temps fictif
|
||||
if (currentTime > DURATION) {
|
||||
currentTime = 0; // boucle simple
|
||||
}
|
||||
updateVinylRotation();
|
||||
animationFrameId = requestAnimationFrame(update);
|
||||
}
|
||||
}
|
||||
update();
|
||||
}
|
||||
|
||||
function stopVinylRotation() {
|
||||
cancelAnimationFrame(animationFrameId);
|
||||
}
|
||||
|
||||
function updateVinylRotation() {
|
||||
currentRotation = (currentTime / DURATION) * 360 * rotationsPerTrack;
|
||||
vinyl.style.transform = `rotate(${currentRotation}deg)`;
|
||||
}
|
||||
|
||||
// Exemple : lancer / arrêter à chaque clic
|
||||
vinyl.addEventListener('click', () => {
|
||||
if (isPlaying) {
|
||||
stopTrack();
|
||||
} else {
|
||||
playTrack();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const express = require('express');
|
||||
/* const express = require('express');
|
||||
const { createServer } = require('node:http');
|
||||
// socket
|
||||
const { Server } = require('socket.io');
|
||||
@ -69,4 +69,55 @@ io.on('connection', (socket) => {
|
||||
|
||||
server.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`)
|
||||
})
|
||||
}) */
|
||||
|
||||
const express = require('express');
|
||||
const http = require('http');
|
||||
const socketIo = require('socket.io');
|
||||
const player = require('play-sound')({ player: 'mpg123' }); // forcer mpg123
|
||||
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
const io = socketIo(server);
|
||||
|
||||
app.use(express.static('public')); // dossier public pour fichiers front
|
||||
|
||||
let currentAudioProcess = null;
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log('Client connecté');
|
||||
|
||||
socket.on('play-sound', (filename) => {
|
||||
console.log('Lecture demandée:', filename);
|
||||
|
||||
// Stopper si un son est déjà joué
|
||||
if (currentAudioProcess) {
|
||||
currentAudioProcess.kill();
|
||||
currentAudioProcess = null;
|
||||
}
|
||||
|
||||
// Lancer la lecture avec mpg123
|
||||
currentAudioProcess = player.play(`public/sounds/${filename}`, (err) => {
|
||||
if (err && !err.killed) {
|
||||
console.error('Erreur lecture audio:', err);
|
||||
}
|
||||
currentAudioProcess = null;
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('stop-sound', () => {
|
||||
if (currentAudioProcess) {
|
||||
currentAudioProcess.kill();
|
||||
currentAudioProcess = null;
|
||||
console.log('Lecture arrêtée');
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
console.log('Client déconnecté');
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(3000, () => {
|
||||
console.log('Serveur démarré sur http://localhost:3000');
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user