未捕获的TypeError:无法读取null的属性'_read'[socket.io-stream]

时间:2018-11-24 09:23:11

标签: javascript node.js socket.io audio-streaming socketstream

我正在写音乐播放器。在此播放器中,服务器使用socket.iosocket.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);
    });

0 个答案:

没有答案