繁体   English   中英

使用Node.js在RPi上播放本地文件

[英]Playing a local file on RPi with Node.js

我有一个带有Node.js应用程序的Raspberry Pi设置,当它看到来自Amazon Dash Button的按钮时会响应。 原本应该是来自https://github.com/initialstate/silent-doorbell的无声门铃,但我想让它播放本地声音文件。 我觉得应该很容易,但是我对编码的经验不足让我只是尝试在互联网上找到的新东西。

我可以使用以下内容从终端播放文件,它播放得很好:

$ omxplayer example.mp3

但是,无论我如何尝试将它放在Node.js应用程序中,并在按下Dash按钮时触发它,它将无法工作。

var dash_button = require('node-dash-button'),
    dash = dash_button('XX:XX:XX:XX:XX:XX'), //REPLACE WITH YOUR ADDRESS
    exec = require('child_process').exec;
    Omx = require('node-omxplayer');
    player = Omx('~/node_modules/node-dash-button/example.mp3');

let spawn = require('child_process').spawn;

dash.on('detected', function() {
    console.log('Button pushed!');
    player.play();
});

当我使用上面的最新版本运行时,我得到了这个:

/home/pi/node_modules/node-dash-button/doorbell.js:7
let spawn = require('child_process').spawn;
    ^^^^^
SyntaxError: Unexpected identifier
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

按照@Quentin的建议,使用本网站上的主要版本升级说明将Node.js升级到最新版本http://thisdavej.com/upgrading-to-more-recent-versions-of-node-js-on-the -raspberry-pi /我能够超越这个。 现在我无法理解如何正确使用omxplayer。 在Node.js升级后运行与上面相同的代码时,我现在在按下Amazon Dash按钮然后崩溃应用程序后出现此错误:

pi@raspberrypi:~/node_modules/node-dash-button $ sudo node doorbell.js 
Button pushed!
/home/pi/node_modules/node-omxplayer/index.js:103
                        throw new Error('Player is closed.');
                        ^

Error: Player is closed.
    at writeStdin (/home/pi/node_modules/node-omxplayer/index.js:103:10)
    at EventEmitter.Omx.omxplayer.play (/home/pi/node_modules/node-omxplayer/index.js:133:27)
    at Readable.<anonymous> (/home/pi/node_modules/node-dash-button/doorbell.js:13:12)
    at emitOne (events.js:96:13)
    at Readable.emit (events.js:188:7)
    at PcapSession.<anonymous> (/home/pi/node_modules/node-dash-button/index.js:87:28)
    at emitOne (events.js:96:13)
    at PcapSession.emit (events.js:188:7)
    at PcapSession.on_packet_ready (/home/pi/node_modules/node-dash-button/node_modules/pcap/pcap.js:99:10)
    at packet_ready (/home/pi/node_modules/node-dash-button/node_modules/pcap/pcap.js:44:14)

我尝试了一些不同的尝试让玩家产生没有运气。 index.js文件引用了提及使用player.running命令,但在尝试使用此命令时仍然会出现播放器关闭错误。

您使用的是早于4.x系列的Node版本。

因此,它将let视为标识符而不是关键字,因此它不会期望它紧跟另一个标识符( spawn )。

将Node的安装升级到当前版本。


或者,使用不同的变量声明,例如var

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM