Я пишу музыкальный проигрыватель.В этом проигрывателе сервер передает потоковую музыку клиенту, используя socket.io
и socket.io-stream
.Когда я запускаю сервер и играю песню, передаваемую с сервера, она работает, но когда я запрашиваю новую песню (используя следующую или предыдущую функцию), я получаю эту ошибку:
Uncaught TypeError: Cannot read property '_read' of null
at IOStream._read (socket.io-stream.js:255)
at IOStream.Readable.read (socket.io-stream.js:4647)
at resume_ (socket.io-stream.js:4998)
at afterTickTwo (socket.io-stream.js:3978)
at Item.run (socket.io-stream.js:4150)
at drainQueue (socket.io-stream.js:4120)
Вот клиенткод:
play() {
if (this.sound) {
let controlPanelObj = this.controlPanel;
let infoBarObj = this.infoBar;
Array.from(controlPanelObj.classList).find((element) => {
return controlPanelObj.classList.add('active');
});
Array.from(infoBarObj.classList).find((element) => {
return infoBarObj.classList.add('active');
});
this.sound.play();
console.log('playing');
}
}
pause() {
if (this.sound) {
let controlPanelObj = this.controlPanel,
infoBarObj = this.infoBar;
Array.from(controlPanelObj.classList).find((element) => {
return controlPanelObj.classList.remove('active');
});
Array.from(infoBarObj.classList).find((element) => {
return infoBarObj.classList.remove('active');
});
this.sound.pause();
console.log('stopped')
}
}
next() {
if (this.sound) {
index < song_list.length-1 ? index=0 : index++;
console.log('next');
this.pause();
socket.emit("get_song", this.songs[index]);
ss(socket).on('audio-stream', (stream, data) => {
let parts = [];
stream.on('data', (chunk) =>{
parts.push(chunk);
});
stream.on('end', () =>{
console.log((window.URL || window.webkitURL).createObjectURL(new Blob(parts)));
this.sound.changeSrc((window.URL || window.webkitURL).createObjectURL(new Blob(parts)));
console.log(this.sound);
this.play();
});
});
}
}
previous() {
if (this.sound) {
index === 0 ? index=0 : index--;
this.pause();
socket.emit("get_song", this.songs[index]);
ss(socket).on('audio-stream', (stream, data) => {
let parts = [];
stream.on('data', (chunk) =>{
parts.push(chunk);
});
stream.on('end', () =>{
console.log((window.URL || window.webkitURL).createObjectURL(new Blob(parts)));
this.sound.changeSrc((window.URL || window.webkitURL).createObjectURL(new Blob(parts)));
console.log(this.sound);
this.play();
});
});
}
}
// on startup load function is called
load() {
console.log('loaded');
socket.emit("get_song", this.songs[index]);
ss(socket).on('audio-stream', (stream, data) => {
let parts = [];
stream.on('data', (chunk) =>{
parts.push(chunk);
});
stream.on('end', () =>{
console.log((window.URL || window.webkitURL).createObjectURL(new Blob(parts)));
this.sound = new Howl({
src: (window.URL || window.webkitURL).createObjectURL(new Blob(parts)),
format: ['mp3'],
onend: this.next()
});
console.log(this.sound);
});
});
}
и код сервера:
socket.on('get_song', (filename) => {
console.log("filename: " + filename);
let file_path = path.join(__dirname, '/public/music', filename);
console.log('path: ' + file_path);
let stream = ss.createStream();
ss(socket).emit('audio-stream', stream, { name: file_path });
fs.createReadStream(file_path).pipe(stream);
});