RaspberryPiとpythonを使って、音を鳴らしたいです。
LEDパネルと連携し、分と秒を表示しています。
ちょうどの秒(00秒)になったタイミングで音を鳴らしたいです。
音の再生はaplayで実行しています。
コンストラクタに設定した際は音がなります。
(ソースコード内の「# ★★★」)
しかし、メソッド内での条件ではうまく再生されません。
(ソースコード内の「# ■■■」)
pythonについては詳しくないため、ご質問した次第です。
ご教示いただけないでしょうか?
・参考にしたサイト
https://a-tak.com/blog/2017/03/05/raspberry-pi-led-clock-4/
https://qiita.com/Nyanpy/items/cb4ea8dc4dc01fe56918
・環境
Raspberry Pi Model 3 B+
Python 3
Python
1#!/usr/bin/env python3 2# coding: UTF-8 3# Display a runtext with double-buffering. 4from samplebase import SampleBase 5from rgbmatrix import graphics 6from datetime import datetime 7import time 8from logging import getLogger, StreamHandler, DEBUG 9 10import subprocess 11 12logger = getLogger(__name__) 13handler = StreamHandler() 14handler.setLevel(DEBUG) 15logger.setLevel(DEBUG) 16logger.addHandler(handler) 17 18# 音をだすクラス 19class SoundClass: 20 # 音を再生 21 def sound_play(self): 22 subprocess.call("aplay /home/pi/Desktop/rpi-rgb-led-matrix/bindings/python/samples/sound.wav", shell=True) 23 24class RunText(SampleBase): 25 26 # コンストラクタ 27 def __init__(self, *args, **kwargs): 28 super(RunText, self).__init__(*args, **kwargs) 29 self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default="21:52") 30 31 # 音を再生 32 # ★★★ 33 sound_class = SoundClass() 34 sound_class.sound_play() 35 36 def run(self): 37 # LEDパネルに描画するcanvasを作成 38 offscreen_canvas = self.matrix.CreateFrameCanvas() 39 font = graphics.Font() 40 41 # フォントを読み込み 42 font.LoadFont("../../../fonts/10x20.bdf") 43 # 出力する文字の色を設定 44 textColor = graphics.Color(100, 100, 255) 45 46 sound_class = SoundClass() 47 48 while True: 49 # 現在時刻を取得 50 now_time = datetime.now() 51 52 # 秒のみ取得 53 s = now_time.second 54 55 # ちょうどの時間に音を出す 56 if s == 00: 57 # メソッド呼び出し 58 # ■■■ 59 sound_class.sound_play() 60 61 logger.debug(now_time.strftime('%M:%S')) 62 offscreen_canvas.Clear() 63 graphics.DrawText(offscreen_canvas, font, 6, 22, textColor, now_time.strftime('%M:%S')) 64 65 #1秒ごとに実行 66 time.sleep(1) 67 68 #表示する 69 self.matrix.SwapOnVSync(offscreen_canvas) 70 71# Main function 72if __name__ == "__main__": 73 run_text = RunText() 74 75 if (not run_text.process()): 76 run_text.print_help()
・00秒になった時のエラー内容
ALSA lib confmisc.c:767:(parse_card) cannot find card '0' ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: そのようなファイルやディレクトリはありません ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: そのようなファイルやディレクトリはありません ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: そのようなファイルやディレクトリはありません ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: そのようなファイルやディレクトリはありません ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM default aplay: main:788: audio open error: そのようなファイルやディレクトリはありません

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/15 01:01