『音律魔装《冥響輪舞》』は、指定した曲を指定回数反復再生する音楽プレイヤーです。
>>GUI外観 >>使い方以下は『音律魔装《冥響輪舞》』 最新版 のソースコード全文です:
import tkinter as tk
from tkinter import filedialog, messagebox
from pathlib import Path
import pygame.mixer
import time
import threading
class MusicPlayer:
def __init__(self, root):
self.root = root
self.root.title("音楽プレイヤー - 音律魔装《冥響輪舞》 ver.2.0")
pygame.mixer.init()
# --- 状態変数 ---
self.file_path = ""
self.play_count = 1
self.current_play = 0
self.is_playing = False
self.is_paused = False
self.pause_pos = 0.0
self.is_transparent = False # 透過状態フラグ
# --- UI構築 ---
self.label = tk.Label(root, text="音楽ファイルを選択してください", font=("Arial", 10))
self.label.pack(pady=10)
self.select_button = tk.Button(root, text="ファイルを選択", command=self.load_file)
self.select_button.pack()
# 再生回数入力(横並び)
count_frame = tk.Frame(root)
count_frame.pack(pady=5)
self.count_label = tk.Label(count_frame, text="再生回数 (1-100):")
self.count_label.pack(side=tk.LEFT)
self.count_entry = tk.Entry(count_frame, width=5, justify="center")
self.count_entry.pack(side=tk.LEFT, padx=5)
self.count_entry.insert(0, "1")
# 操作ボタン(横並び)
button_frame = tk.Frame(root)
button_frame.pack(pady=8)
self.play_button = tk.Button(button_frame, text="再生", command=self.start_playback, width=10)
self.play_button.pack(side=tk.LEFT, padx=5)
self.pause_button = tk.Button(button_frame, text="一時停止", command=self.pause_music, state=tk.DISABLED, width=10)
self.pause_button.pack(side=tk.LEFT, padx=5)
self.stop_button = tk.Button(button_frame, text="再生終了", command=self.stop_playback, state=tk.DISABLED, width=10)
self.stop_button.pack(side=tk.LEFT, padx=5)
# ステータス表示(色付き)
self.status_label = tk.Label(root, text="停止中", font=("Arial", 10), fg="red")
self.status_label.pack(pady=10)
# バージョン表記(クリックで透過切替)
self.version_label = tk.Label(root, text="version 2.0", font=("Arial", 7), fg="gray", cursor="hand2")
self.version_label.pack(side=tk.BOTTOM, pady=5)
self.version_label.bind("<Button-1>", self.toggle_transparency)
# 閉じる処理
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
# -----------------------------------------------------
# ファイル選択
# -----------------------------------------------------
def load_file(self):
path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.mp3;*.aac")])
if path:
self.file_path = path
self.label.config(text=f"選択されたファイル: {Path(path).name}")
# -----------------------------------------------------
# 再生開始
# -----------------------------------------------------
def start_playback(self):
if not self.file_path:
messagebox.showwarning("警告", "ファイルを選択してください")
return
try:
count = int(self.count_entry.get())
if not 1 <= count <= 100:
raise ValueError
self.play_count = count
except ValueError:
messagebox.showerror("エラー", "再生回数は1から100の間で入力してください")
return
self.current_play = 0
self.pause_pos = 0
self.is_playing = True
self.is_paused = False
self.set_button_state(tk.DISABLED, tk.NORMAL, tk.NORMAL)
self.update_status()
threading.Thread(target=self.play_loop, daemon=True).start()
# -----------------------------------------------------
# 再生ループ
# -----------------------------------------------------
def play_loop(self):
while self.is_playing and self.current_play < self.play_count:
self.play_once()
if self.is_playing and not self.is_paused:
self.current_play += 1
self.pause_pos = 0
self.update_status()
self.is_playing = False
self.reset_buttons()
self.update_status()
# -----------------------------------------------------
# 1回分再生
# -----------------------------------------------------
def play_once(self):
try:
pygame.mixer.music.load(self.file_path)
pygame.mixer.music.play(start=self.pause_pos)
while pygame.mixer.music.get_busy() and self.is_playing:
if self.is_paused:
pygame.mixer.music.pause()
while self.is_paused and self.is_playing:
time.sleep(0.2)
pygame.mixer.music.unpause()
time.sleep(0.2)
except Exception as e:
messagebox.showerror("再生エラー", str(e))
self.is_playing = False
# -----------------------------------------------------
# 一時停止
# -----------------------------------------------------
def pause_music(self):
if not self.is_playing:
return
if not self.is_paused:
self.pause_pos = pygame.mixer.music.get_pos() / 1000.0
pygame.mixer.music.pause()
self.is_paused = True
self.pause_button.config(text="再開")
self.status_label.config(text="一時停止中", fg="orange")
else:
pygame.mixer.music.unpause()
self.is_paused = False
self.pause_button.config(text="一時停止")
self.update_status()
# -----------------------------------------------------
# 停止
# -----------------------------------------------------
def stop_playback(self):
self.is_playing = False
self.is_paused = False
self.pause_pos = 0
pygame.mixer.music.stop()
self.reset_buttons()
self.update_status()
# -----------------------------------------------------
# 透過切替
# -----------------------------------------------------
def toggle_transparency(self, event=None):
if not self.is_transparent:
self.root.attributes("-alpha", 0.2)
self.version_label.config(fg="blue")
self.is_transparent = True
else:
self.root.attributes("-alpha", 1.0)
self.version_label.config(fg="gray")
self.is_transparent = False
# -----------------------------------------------------
# ボタン制御
# -----------------------------------------------------
def set_button_state(self, play, pause, stop):
self.play_button.config(state=play)
self.pause_button.config(state=pause)
self.stop_button.config(state=stop)
def reset_buttons(self):
self.set_button_state(tk.NORMAL, tk.DISABLED, tk.DISABLED)
self.pause_button.config(text="一時停止")
# -----------------------------------------------------
# 状態表示
# -----------------------------------------------------
def update_status(self):
if not self.is_playing:
self.status_label.config(text="停止中", fg="red")
else:
remaining = max(self.play_count - self.current_play, 0)
self.status_label.config(
text=f"再生中: {self.current_play}/{self.play_count} 回 残り: {remaining} 回",
fg="green"
)
# -----------------------------------------------------
# 終了
# -----------------------------------------------------
def on_closing(self):
self.is_playing = False
pygame.mixer.music.stop()
pygame.mixer.quit()
self.root.destroy()
# ---------------------------------------------------------
# メイン
# ---------------------------------------------------------
if __name__ == "__main__":
root = tk.Tk()
##タイトルバーアイコン表示用
data = '''R0lGODdhEAAQAIQAABgjKg8aIwcTHNva1E9WWjM8Qvv694WJiQAGD7W2tHR4ecrKxTpDSFxiZSMsM+7t58PDvZOXlqWop0FJTmVrbis0O3+EhOLf2FZcYJygoeXk3gAAAAAAAAAAAAAAAAAAACwAAAAAEAAQAEAIuwABCBwYoODAgwIaQAgAQIACDBUyHEBAYAEDhgEmGCCAIKOCBA0UXDjYUMEDCgAYFpCgoQBDhAUSDJg5IEJKggUGOCgpgQGBiQ0WCBAYYEADjQYKHEhQAEECBUMPBhBQgAABBwJekkwpwAGDAg23NmzwIILLAAxkuhwoYMHEABQMyI2AAMKBoQkXluSoVEACAwcKMhgQoEKCBwQGAB7q4KUAAgZ8EigoAIGFAVsfLxiwYLOExmKJCsi6NSAAOw=='''
root.tk.call('wm', 'iconphoto', root._w, tk.PhotoImage(data=data))
####
app = MusicPlayer(root)
root.mainloop()