Raspbian: 時刻に合わせて自動的に BGM を再生

概要

時刻に合わせて自動的に BGM を切り替えるシステムを構築した。

最初は Raspberry Pi + Volumio(アラーム機能) で運用していたのが、BGM が止まったり、指定時刻になっても切り替わらないなど、 不安定すぎて使い物にならず。

Raspberry Pi + Raspbian + MPD + cron の構成に切り替えることにした。現在3日ほど運用しているが全く問題無く安定稼働中。

具体的な運用として現在は、朝(6:00~)、昼(12:00~)、晩(20:00~)で 自動的に BGM を変更している。

 

MPD のインストール

下記の記事を参照。


BGM の準備

/var/lib/mpd/music ディレクトリに BGM に使用する音楽ファイルをコピーしておく。ディレクトリを作ってその下に置いても OK(デフォルトでは3層まで)

一応パーミッションを変更しておく。

$ sudo chmod -R 666 /var/lib/mpd/music

music ディレクトsamba で共有しておくと後の管理が楽。

 


プレイリストの準備

登録

morning,noon,night という名前のプレイリストを用意する。

プレイリストは標準的な m3u 形式で、/var/lib/mpd/playlist ディレクトリに配置する。

m3u 形式はプレーンテキストで、ファイル名を羅列したもの。

■ morning.m3u の例。

高原の朝/01 春の訪れ (清里).mp3
高原の朝/02 雨上がりの朝 (清里・美し森).mp3
高原の朝/03 安らぎの森 (富士山麓).mp3
高原の朝/04 高原のせせらぎ (軽井沢).mp3
高原の朝/05 新緑の季節 (富士山麓).mp3 

 

確認

登録したプレイリストの一覧。

$ mpc lsplaylists 

 

プレイリストの内容。

$ mpc playlist <プレイリスト名>

 

mpc クライアントの利用

GMPC 等のプレイリスト作成機能を持った mpc クライアントを使用すると GUI で操作できて楽。

GMPC は linux 以外に Windows 版もある。

 

繰り返し設定

キューを繰り返し再生するように設定。

$ mpc repeat on

 

cron の設定

cron で指定時刻に mpc コマンドを実行し MPD をコントロールする。

$ crontab -e

 

■ 設定例

# m h dom mon dow command
00 06 * * * mpc clear; mpc load morning; mpc play
00 12 * * * mpc clear; mpc load noon; mpc play
00 20 * * * mpc clear; mpc load night; mpc play

 mpc clear でカレントプレイリスト(キュー)をクリアし、mpc load でプレイリストをキューに登録、mpc play で再生する。

 

mpc コマンド

Commands:
mpc   # Display status
mpc add <uri>   # Add a song to the current playlist
mpc crop   # Remove all but the currently playing song
mpc current   # Show the currently playing song
mpc del <position>   # Remove a song from the current playlist
mpc play [<position>]   # Start playing at <position> (default: 1)
mpc next   # Play the next song in the current playlist
mpc prev   # Play the previous song in the current playlist
mpc pause   # Pauses the currently playing song
mpc toggle   # Toggles Play/Pause, plays if stopped
mpc cdprev   # Compact disk player-like previous command
mpc stop   # Stop the currently playing playlists
mpc seek [+-][HH:MM:SS]|<0-100>%    # Seeks to the specified position
mpc clear   # Clear the current playlist
mpc outputs   # Show the current outputs
mpc enable [only] <output # or name> [...]   # Enable output(s)
mpc disable [only] <output # or name> [...]   # Disable output(s)
mpc toggleoutput <output # or name> [...]   # Toggle output(s)
mpc shuffle   # Shuffle the current playlist
mpc move <from> <to>   # Move song in playlist
mpc playlist [<playlist>]   # Print <playlist>
mpc listall   # [<file>] List all songs in the music dir
mpc ls [<directory>]   # List the contents of <directory>
mpc lsplaylists   # List currently available playlists
mpc load <file>   # Load <file> as a playlist
mpc insert <uri>   # Insert a song to the current playlist after the current track
mpc save <file>   # Save a playlist as <file>
mpc rm <file>   # Remove a playlist
mpc volume [+-]<num>   # Set volume to <num> or adjusts by [+-]<num>
mpc repeat <on|off>   # Toggle repeat mode, or specify state
mpc random <on|off>   # Toggle random mode, or specify state
mpc single <on|off>   # Toggle single mode, or specify state
mpc consume <on|off>   # Toggle consume mode, or specify state
mpc search <type> <query>   # Search for a song
mpc find <type> <query>   # Find a song (exact match)
mpc findadd <type> <query>   # Find songs and add them to the current playlist
mpc list <type> [<type> <query>]   # Show all tags of <type>
mpc crossfade [<seconds>]   # Set and display crossfade settings
mpc clearerror   # Clear the current error
mpc mixrampdb [<dB>]   # Set and display mixrampdb settings
mpc mixrampdelay [<seconds>]   # Set and display mixrampdelay settings
mpc update [<path>]   # Scan music directory for updates
mpc sticker <uri> <get|set|list|delete|find> [args..]   # Sticker management
mpc stats   # Display statistics about MPD
mpc version   # Report version of MPD
mpc idle [events]   # Idle until an event occurs
mpc idleloop [events]   # Continuously idle until an event occurs
mpc replaygain [off|track|album]   # Set or display the replay gain mode
mpc channels   # List the channels that other clients have subscribed to.
mpc sendmessage <channel> <message> ### Send a message to the specified channel.
mpc waitmessage <channel>   # Wait for at least one message on the specified channel.
mpc subscribe <channel>   # Subscribe to the specified channel and continuously receive messages.