NodeJS 使用castv 2-client设置音量级别

vatpfxk5  于 2023-01-12  发布在  Node.js
关注(0)|答案(2)|浏览(156)

我用castv2-clientgoogle-tts-api通过ip地址输出音频信息到我的google home。它工作正常,但是音量有点大,我不知道如何降低音量。如果我在google home上手动降低音量,应用程序会覆盖它,它仍然很大。我怎么才能把音量设置得更低?
此外,console. log("VOLUME",音量)输出

{
  controlType: 'master',
  level: 1,
  muted: false,
  stepInterval: 0.019999999552965164
}
var Client = require("castv2-client").Client;
var DefaultMediaReceiver = require("castv2-client").DefaultMediaReceiver;
const googleTTS = require("google-tts-api");
var cron = require("node-cron");

function broadcastMessage(message) {
  var App = {
    playin: false,
    DeviceIp: "",
    Player: null,
    GoogleHome: function (host, url) {
      var client = new Client();
      client.connect(host, function () {
        client.launch(DefaultMediaReceiver, function (err, player) {
          client.setVolume({ level: 1 }, function (err, volume) {
            console.log("VOLUME", volume);
            if (err) {
              // Handle error as appropriate
              console.log("Error on setVolume:", err);
            } else {
              console.log("Sending new alert");
            }
          });

          var media = {
            contentId: url,
            contentType: "audio/mp3",
            streamType: "BUFFERED",
          };
          App.Player = player;
          App.Player.load(media, { autoplay: true }, function (err, status) {
            App.Player.on("status", function (status) {
              if (status.playerState === "IDLE" && App.playin === false) {
                client.close();
              }
            });
          });
        });
      });
      client.on("error", function (err) {
        console.log("Error: %s", err.message);
        client.close();
      });
    },
    run: function (ip, text) {
      App.DeviceIp = ip;
      const url = googleTTS.getAudioUrl(text, {
        lang: "en-US",
        slow: false,
        host: "https://translate.google.com",
      });
      App.GoogleHome(App.DeviceIp, url, function (res) {
        console.log(res);
      });
    },
    broadcast: function (text) {
      const ip = "192.168.0.17";
      App.run(ip, text);
    },
  };

  App.broadcast(message);
  setTimeout(() => {
    App.broadcast(message);
  }, 4000);
}

module.exports = {
  broadcastMessage,
};
broadcastMessage("Incoming message");
92dk7w1h

92dk7w1h1#

据我所知,你在代码中设置了音量为1,这是最大音量,所以你的音频将以最大音量播放,这是你设置音量的地方:

client.setVolume({ level: 1 }, function (err, volume) {
    console.log("VOLUME", volume);
    if (err) {
      // Handle error as appropriate
      console.log("Error on setVolume:", err);
    } else {
      console.log("Sending new alert");
    }
});

只需删除此代码,您的消息将以您在设备上手动设置的音量播放。
另外:如果您打算设置设备的音量,则应等待回调返回,然后才发送消息。

client.setVolume({ level: 1 }, function (err, volume) {
    console.log("VOLUME", volume);
    if (err) {
      // Handle error as appropriate
      console.log("Error on setVolume:", err);
    } else {
      // Here you should send the message, since only now you know for sure that the volume was set.
    }
});
i86rm4rw

i86rm4rw2#

解决方案是我必须在client.setVolume({ level: 1 }调用之前定义值,所以基本上

const volume = 0.6;
client.setVolume({ level: volume }

相关问题