树莓派使用node expess web应用调用mpg123播放音乐

相关代码:https://github.com/cloudswave/pi3-script/tree/master/node_web
原理:使用node的child_process模块对子进程进行操作,控制mgp123

spawnObj = child_process.spawn('mpg123', ['-R'], {encoding: 'utf-8'});
spawnObj.stdin.write(`L ${url}\n`); //mpg123 播放
spawnObj.stdin.write('Q\n');//退出播放

主程序:

"use strict"
var express = require('express');
var app = express();
var child_process = require('child_process');
var spawnObj = null;
var current_status = "stop";
app.get('/', function(req, res) {
    res.send('Hello World node11!');
});
app.get('/music/:status/:url', function(req, res) {// url是音乐的外链地址
    res.send('music: ' + req.params.status + ':' + req.params.url);
    let status = req.params.status;
    let url = decodeURIComponent(req.params.url);
    if (!spawnObj) {
        spawnObj = child_process.spawn('mpg123', ['-R'], {
            encoding: 'utf-8'
        });
        spawnObj.stdout.on('data', function(chunk) {
            console.log(chunk.toString());
        });
        spawnObj.stderr.on('data', (data) => {
            console.log(data);
        });
        spawnObj.on('close', function(code) {
            console.log('close code : ' + code);
        });
        spawnObj.on('exit', (code) => {
            console.log('exit code : ' + code);
        });
        spawnObj.stdin.write('V 60\n');
    }
    try {
        
        switch (status) {
            case 'play':
                    spawnObj.stdin.write(`L ${url}\n`);
                break;
            case 'stop':
                if(spawnObj) {
                    spawnObj.stdin.write('Q\n');
                    spawnObj.kill();
                    spawnObj = null;                    
                }

                break;
            case 'pause':
                if(spawnObj) {
                    spawnObj.stdin.write('P\n');
                }
                break;
            case 'resume':
                if(spawnObj)
                    spawnObj.stdin.write('P\n');
            default:
                // code
        }
        current_status = status;
    } catch (e) {
        console.error('error:',e);
    }

});
var server = app.listen(3000, function() {
    var host = server.address().address;
    var port = server.address().port;
    console.log('Example app listening at http://%s:%s', host, port);
});

>演示<
参考了python的实现:https://github.com/cloudswave/musicbox

你可能感兴趣的:(树莓派使用node expess web应用调用mpg123播放音乐)