前回から2か月はすぎかな。
はぁ。。
今回はイベントドリブンでなくて、ビジーループでMP3再生終了を検知。
mp3ファイルはwindowsからコピーしたもの。
C:\Users\Public\Music\Sample Music\MaidWithTheFlaxenHair.mp3
ちょっと、試行錯誤したけど、音だし確認はOK。
次の機会にはイベントドリブンで実装。今のペースだと春くらいかな。。
#!/usr/bin/env python3
# -*- coding: utf8 -*-
# http://www.pygame.org/docs/tut/ImportInit.html
# The first line here is the only necessary one.
# It imports all the available pygame modules into the pygame package.
# The second line is optional, and puts a limited set of constants and functions into the global namespace of your script.
import time
import sys, pygame
from pygame.locals import *
#----------------------------------------------------#
#■変数初期化 #
#----------------------------------------------------#
img_file= "/home/pi/MaidWithTheFlaxenHair.mp3"
#----------------------------------------------------#
#■ まえ処理 #
#----------------------------------------------------#
print('pygame.version.vernum', pygame.version.vernum)
print("file path", img_file)
# リソース初期化
ret_api = pygame.init() # initialize all imported pygame modules
print("pygame.init(),success {0}, failed {1}".format(ret_api[0], ret_api[1]))
# CD音質で初期化
# サンプリング周波数 44.1KHz
# 量子化ビット 16bit
# チャンネル数 2チャンネル
# バッファサイズ デフォルト
pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=4096)
pygame.mixer.music.load(img_file)
#----------------------------------------------------#
#■ 再生 #
#----------------------------------------------------#
pygame.mixer.music.play(loops=0, start=0.0) # 再生開始
while True:
time.sleep(1) # システムに制御を一時返す
pos_playtime = pygame.mixer.music.get_pos()
if (pos_playtime == -1): # 再生していない状態(再生完了)は-1
print("STOP", pygame.mixer.music.get_busy())
break
else:
print("get the music play time", pos_playtime,
"get_volume", pygame.mixer.music.get_volume(),
"get_busy", pygame.mixer.music.get_busy())
#----------------------------------------------------#
#■ あと処理 #
#----------------------------------------------------#
pygame.quit
# end of file
※それなりに動作するとは思いますが、自己責任でお願いします。
株式会社スプレッド