2012-10-04 47 views
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) 

回答

2

不要試圖測試或調試一些想到的想法:

使用mplayer的從屬模式。通過在啓動時爲其提供-slave標誌來做到這一點。有關更多信息,請參閱http://www.mplayerhq.hu/DOCS/tech/slave.txt

設置代碼以顯示mplayers想告訴你什麼。可能比觀察套接字狀態更有意義。

var spawn = require('child_process').spawn; 
var mplayer; 
var file = '/Users/snorpey/Music/Blink-182/Untitled/01 Feeling This.m4a'; 

mplayer = spawn('mplayer', [ '-slave', file ]); 
mplayer.on('exit', function() { console.log('EXIT.'); }); 

// obviously you'll want something smarter than just logging... 
mplayer.stdout.on('data', function (data) { console.log('mplayer stdout: ' + data); }); 
mplayer.stderr.on('data', function (data) { console.log('mplayer stderr: ' + data); }); 

setTimeout(pause, 5000); 
setTimeout(pause, 9000); 

function pause() { 
    console.log('PAUSE'); 
    mplayer.stdin.write('pausing\n'); 
} 
+0

這似乎解決了這個問題。使用''''''''''''標誌時我沒有遇到錯誤。真棒。謝謝! – snorpey

相關問題