LoginSignup
18
5

More than 1 year has passed since last update.

google-home-notifierの代替となりそうなgoogle-home-playerを試してみる

Last updated at Posted at 2021-12-16

はじめに

Google Home発売当初、Google HomeハックのHello Worldとも言える「Google Homeを喋らせてみた」ハックで誰もが使用したであろう「google-home-notifier」ですが、動かなくなったりメンテされなくなったりで、もはや多くの方がGoogle Homeを喋らせることをやめてしまったのではないでしょうか。

そんな折ふとnpmを眺めていたら「google-home-player」というパッケージがあり、READMEでもgoogle-home-notifierの代替とあったので試してみました。

google-home-player

google-home-notifierはcallback形式の書き方でしたが、google-home-playerはPromiseで書けます。もちろんasync/awaitでも。
そのおかげもあってかgoogle-home-notifierに比べ大分すっきり書ける気がします。

内部の実装は基本的にはgoogle-home-notifierと同様「castv2-client」でGoogle Home / Google Nestへキャストして「google-tts-api」で生成した発話mp3 URLを再生しているようです。

また更新はパッケージのアップデート等軽微なものですが、最終更新がひと月前とちょこちょこメンテナンスされていそうです。

サンプルコード

機能をコメントを交えたコードで紹介します。

Google Home / Google Nestを喋らせる
const GoogleHomePlayer = require("google-home-player")

const ip = "x.x.x.x" // Google Home / Google NestのIPアドレスを入力
const lang = "ja"

const googleHome = new GoogleHomePlayer(ip, lang)

; (async () => {
    await googleHome.say("first text")
    await googleHome.say("second text", "en") // 第二引数で言語を指定
    await googleHome.say("final text", "en", true) // 第三引数でslowの有効/無効を指定
})()

ちなみにlangslowはインスタンス作成時にもデフォルトを指定できます。

インスタンス作成時にデフォルト指定
const GoogleHomePlayer = require("google-home-player")

const ip = "x.x.x.x" // Google Home / Google NestのIPアドレスを入力
const lang = "ja"
const slow = true

let googleHome
googleHome = new GoogleHomePlayer(ip)             // lang: "en", slow: false
googleHome = new GoogleHomePlayer(ip, lang)       // lang: "ja", slow: false
googleHome = new GoogleHomePlayer(ip, lang, slow) // lang: "ja", slow: true

google-home-notifier同様、音楽再生もいけます。

音楽再生
const GoogleHomePlayer = require("google-home-player")

const ip = "x.x.x.x" // Google Home / Google NestのIPアドレスを入力
const lang = "ja"

const googleHome = new GoogleHomePlayer(ip, lang)

; (async () => {
    await googleHome.play("https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3")
})()

おわりに

google-home-playerのおかげで再びGoogle Home / Google Nestを喋らせることができました。
ただgoogle-home-notifierもそうでしたがキャストの仕組みを使っている以上、音楽再生中などに喋らせると音楽が中断されたまま再開しないという動きとなるため、やっぱ使わなそうです…😢

18
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
5