1
我正在嘗試使用child_process.spawn使用node.js程序controlmplayer。如何使用child_process.spawn暫停媒體播放器?
我在暫停播放後遇到問題。由於某些原因,即使mplayer尚未完成播放,也會發送exit
事件。
我在做什麼錯?
下面的代碼工作正常(我可以聽到音樂播放),直到第二次暫停命令。
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)
這似乎解決了這個問題。使用''''''''''''標誌時我沒有遇到錯誤。真棒。謝謝! – snorpey