【改訂版】【PySide6】カルタの問題を読み上げてくれるだけのアプリを作る

はじめに

以前カルタの問題を読み上げてくれるアプリを作りました。
touch-sp.hatenablog.com
その時は音声ファイルの再生に「python-vlc」を使っていました。そのためにVLC playerをインストールする必要もありました。

今回、PySide6に含まれる「QtMultimedia」で音声を再生するようにスクリプトを書き換えました。

Pythonスクリプト

「QtMultimedia」を使うとmediaStatusで再生が終了しているかどうかの判定が可能になります。

もちろんVLC playerのインストールもpython-vlcライブラリのインストールも必要ありません。

おかけでスクリプトがすっきりしました。

import glob
import random
import os
from PySide6 import QtMultimedia
from PySide6.QtCore import Qt, QSize, QEventLoop, QTimer, QUrl
from PySide6.QtWidgets import QMainWindow, QApplication, QWidget, QLabel, QPushButton, QGridLayout, QSizePolicy

class Window(QMainWindow):

    sound_list = []
    sound_num = 0

    player = QtMultimedia.QMediaPlayer()
    audioOutput = QtMultimedia.QAudioOutput()
    
    def __init__(self):
        super().__init__()    
        self.initUI()

    def initUI(self):
        self.player.setAudioOutput(self.audioOutput)

        self.setFixedSize(QSize(600, 300))
        self.setWindowTitle('カルタ')
        self.countryLabel = QLabel('開始')
        self.countryLabel.setAlignment(Qt.AlignCenter)
        self.countryLabel.setStyleSheet('font: 40px; font-weight: bold')

        self.startBtn = QPushButton('はじめ')
        self.startBtn.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        self.startBtn.setStyleSheet('font: 20px; font-weight: bold')
        self.startBtn.clicked.connect(self.push_startBtn)

        self.nextBtn = QPushButton('つぎのカード')
        self.nextBtn.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        self.nextBtn.setStyleSheet('font: 20px; font-weight: bold')
        self.nextBtn.setEnabled(False)
        self.nextBtn.clicked.connect(self.push_nextBtn)

        layout = QGridLayout()
        layout.addWidget(self.countryLabel, 0, 0, 1, 4)
        layout.addWidget(self.startBtn, 1, 0, 1, 1)
        layout.addWidget(self.nextBtn, 1, 1, 1, 3)

        mainWindow = QWidget()
        mainWindow.setLayout(layout)

        self.setCentralWidget(mainWindow)

    def push_startBtn(self):
        self.startBtn.setEnabled(False)
        self.nextBtn.setEnabled(True)
        self.countryLabel.setText('')

        self.sound_list = glob.glob('sounds/*.wav')
        self.sound_num = len(self.sound_list) - 1

        random.shuffle(self.sound_list)

    def push_nextBtn(self):
        self.nextBtn.setEnabled(False)
        self.countryLabel.setText('')
        self.Sound(self.sound_list[self.sound_num])    
        country_name = os.path.splitext(os.path.basename(self.sound_list[self.sound_num]))[0]
        self.countryLabel.setText(country_name)
        self.sound_num -= 1
        if self.sound_num < 0:
            self.startBtn.setEnabled(True)
        else:
            self.nextBtn.setEnabled(True)

    def Sound(self, sound):
        self.player.setSource(QUrl(sound))
        self.player.play()
        while not self.player.mediaStatus() == QtMultimedia.QMediaPlayer.EndOfMedia:
            loop = QEventLoop()
            QTimer.singleShot(10, loop.quit)
            loop.exec()
        
if __name__ == "__main__":
    app = QApplication([])
    ex =Window()
    ex.show()
    app.exec()

動作環境

Windows 11
Python 3.10.5
PySide6==6.3.1
PySide6-Addons==6.3.1
PySide6-Essentials==6.3.1
shiboken6==6.3.1

音声ファイルの作り方

こちらを参照して下さい。
touch-sp.hatenablog.com
touch-sp.hatenablog.com