1

我正在尝试使用 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)
4

1 回答 1

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' );
}
于 2012-10-05T07:54:01.490 回答