1. 程式人生 > >Node.js 操作 OSX 系統麥克風、揚聲器音量

Node.js 操作 OSX 系統麥克風、揚聲器音量

最近幾年 Electron 很火,公司也正好有個專案想做跨平臺客戶端,大家研究了一下就選擇了 Electron,第一次做 js 的專案遇到了不少坑,不過也都一點點解決了。
因為專案中需要對使用者錄音,HTML5 中的 API 又不支援調整麥克風音量,所以就對 Node js 操作 osx 系統麥克風、揚聲器音量了解了一下,一開始在 npm(https://www.npmjs.com/)中找了很多包都是隻能操作揚聲器音量的,無法操作麥克風的音量,本來已經打算放棄了,缺無意間在網上看到 osx 系統下有自己的指令碼——AppleScript,查看了官方文件之後發現 AppleScript 確實可以控制系統的音量(

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW41),之後又到 npm 中搜索一番找到了 osx-volume-controls(https://www.npmjs.com/package/osx-volume-controls)這個包,經過改造終於實現了這個功能

因為要使用到 applescript 這個包,所以需要執行下面的命令:

 npm i -save applescript 

具體類實現程式碼如下:

var applescript = require("applescript");

var scripts = {
  state: "input volume of (get volume settings) & output volume of (get volume settings) & output muted of (get volume settings)",
  volumeState: "output volume of (get volume settings)",
  inputState: "input volume of (get volume settings)",
  outputState: "output volume of (get volume settings)",
  muteState: "output muted of (get volume settings)",
  setOutput: "set volume output volume %s --100%",
  setInput: "set volume input volume %s --100%",
  increase: "set volume output volume (output volume of (get volume settings) + 10) --100%",
  decrease: "set volume output volume (output volume of (get volume settings) - 10) --100%",
  mute: "set volume with output muted",
  unmute: "set volume without output muted"
};

var exec = function (script, callback) {
  if (!callback) callback = function () {};
  applescript.execString(script, callback);
};

var getScript = function (scriptName, param) {
  var script = scripts[scriptName];
  if (typeof param !== "undefined") script = script.replace("%s", param);
  return script;
};

exports.state = function (callback) {
  return exec(getScript("state"), callback);
};

exports.volumeState = function (callback) {
  return exec(getScript("volumeState"), callback);
};

exports.inputState = function (callback) {
  return exec(getScript("inputState"), callback);
};

exports.outputState = function (callback) {
  return exec(getScript("outputState"), callback);
};

exports.muteState = function (callback) {
  return exec(getScript("muteState"), callback);
};

exports.setOutput = function (volume, callback) {
  return exec(getScript("setOutput", volume), callback);
};

exports.setInput = function (volume, callback) {
  return exec(getScript("setInput", volume), callback);
};

exports.increase = function (callback) {
  return exec(getScript("increase"), callback);
};

exports.decrease = function (callback) {
  return exec(getScript("decrease"), callback);
};

exports.mute = function (callback) {
  return exec(getScript("mute"), callback);
};

exports.unmute = function (callback) {
  return exec(getScript("unmute"), callback);
};

使用方法(可以參照osx-volume-controls文件):

    import volume from "../../utils/osxVolume.js";
    volume.setInput(volumecontrol);