1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

OneDriveに保存したMP3ファイルをGoogleホームから再生する

Last updated at Posted at 2024-01-30

はじめに

今回はOneDriveに保存されているMP3ファイルをGoogleホームへキャストし、再生することを試してみました。必要なのはESP8266やESP32というWi-Fiを内蔵したMCUのみです。Arduino開発環境が使えます。
picture.jpg

開発環境

  • Windows 11 Home 22H2
  • Arduino IDE 2.2.1
  • esp8266-google-home-notifier 1.0.7
  • esp8266-google-tts 1.1.0

書き込み

今回はこちらのESP-WROOM-02開発ボードを使いました。スイッチサイエンスより購入できます。USBシリアル変換ICを搭載しているため、USBケーブルで直接PCと接続できます。ただし、書き込むには動作モード切り替え用スイッチを押しながらリセットする必要があります。電源を入れ直す、あるいはリセットスイッチを押すことでリセットできます。失敗してしまう場合は書き込みが始まるまで動作モード切り替え用スイッチを押し続けておく必要があるようです1

OneDrive

まずはMP3ファイルをOneDriveへアップロードし、リンクを知っている全員が閲覧できるように共有します。取得したリンクに移動してみると遷移した先のページから埋め込み用のコードが取得できます2
screenshot.png
埋め込み用のコードは以下のような形になると思います。

https://onedrive.live.com/embed?resid={id}&authkey={key}

これを以下のような形に少し修正し、後述するスケッチへ貼り付けます。

https://onedrive.live.com/download?resid={id}&authkey={key}

スケッチ

こちらのサンプルコードを参考にしました。ライブラリの詳しい使い方はこちらです。関数playにURLを渡すことでMP3ファイルの再生が可能です。また、デバイス名は半角英数字でGoogleホームのアプリにおける表示名を指定します。接続にはmDNSを利用しているようなのでIPアドレスを指定する必要はありません。

google_home_music.ino
#include <ESP8266WiFi.h>
#include <esp8266-google-home-notifier.h>

const char *ssid = "{ssid}"; //put your Wi-Fi SSID
const char *password = "{password}"; //put your Wi-Fi password

const char *name = "{name}"; //put your device name
const char *url = "https://onedrive.live.com/download?resid={id}&authkey={key}"; //put your embed link

GoogleHomeNotifier notifier;


void playMusic() {  
  Serial.println("Finding your device...");
  if (notifier.device(name, "ja") != true) {
    Serial.println(notifier.getLastError());
    return;
  }
  Serial.print("Found your device (");
  Serial.print(notifier.getIPAddress());
  Serial.print(":");
  Serial.print(notifier.getPort());
  Serial.println(")");

  if (notifier.play(url) != true) {
    Serial.println(notifier.getLastError());
    return;
  }
  Serial.println("Done");
}

void setup() {
  Serial.begin(74880); //to avoid text garbling

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Connected to Wi-Fi successfully");
  
  playMusic();
}

void loop() {
}

動作確認

正しくGoogleホームと接続できたら再生が始まります。曲名などは表示されませんが、巻き戻し、早送り、再生、停止などの操作は可能です。ただし、途中でGoogleホームに喋らせてしまうと再生を再開できなくなる、再生が終わってもコールバック関数などが用意されていないなど、連続再生には厳しそうです。

おわりに

通常のキャストと同じようなので用途は限られると思いますが、時報やアラームなどに活用できそうです。

  1. https://qiita.com/shanonim/items/68fab6dc28b72b31a258

  2. https://superuser.com/questions/1548950/html-audio-with-mp3-stored-on-onedrive

1
3
0

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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?