簡體   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