我正在尝试使用 child_process.spawn 通过 node.js 程序控制mplayer 。
暂停后,我无法恢复播放。出于某种原因,exit
即使 mplayer 尚未完成播放,也会发送一个事件。
我究竟做错了什么?
以下代码工作正常(我可以听到音乐播放),直到第二个暂停命令。
var spawn = require( 'child_process' ).spawn;
var mplayer;
var file = '/Users/snorpey/Music/Blink-182/Untitled/01 Feeling This.m4a';
mplayer = spawn( 'mplayer', [ file ] );
mplayer.on( 'exit', function(){ console.log( 'EXIT.' ); } );
setTimeout( pause, 5000 );
setTimeout( pause, 9000 );
function pause()
{
console.log( 'PAUSE', mplayer.stdin );
mplayer.stdin.write( 'p\n' );
}
mplayer.stdin 的第一个输出如下所示:
{ _handle:
{ writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread] },
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: false,
errorEmitted: false,
bytesRead: 0,
_bytesDispatched: 0,
allowHalfOpen: undefined,
writable: true,
readable: false }
第二个是这样的:
{ _handle: null,
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: true,
errorEmitted: false,
bytesRead: 0,
_bytesDispatched: 2,
allowHalfOpen: undefined,
writable: false,
readable: false,
_connecting: false,
_connectQueue: null,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1 }
我收到以下错误:
events.js:66
throw arguments[1]; // Unhandled 'error' event
^
Error: This socket is closed.
at Socket._write (net.js:517:19)
at Socket.write (net.js:509:15)
at Object.pause [as _onTimeout] (/Users/snorpey/Sites/Development/mplayer/pause.js:14:16)
at Timer.list.ontimeout (timers.js:101:19)