LoginSignup
1
6

More than 3 years have passed since last update.

音楽連続再生機能(python )

Last updated at Posted at 2021-03-21

pythonで音源の連続再生機能を作ってみた

import pygame.mixer
from mutagen.mp3 import MP3 as mp3
import time
playlist = ['/test/test1.mp3', '/test/test2.mp3', '/test/test3.mp3']
for p in playlist:
 pygame.mixer.init()
 pygame.mixer.music.load(p)
 mc = mp3(p).info.length #音源の長さを取得
 pygame.mixer.music.play(1) #再生
 time.sleep(mc + 0.5) #誤差があるかもしれないので一応0.5足しておく

mutagenとpygameは下記コマンドでインストール

$ pip install pygame mutagen

ランダム再生

方法1

import pygame.mixer
import sys
import random
from mutagen.mp3 import MP3 as mp3
import time
playlist = ['/test/test1.mp3', '/test/test2.mp3', '/test/test3.mp3']
num = len(playlist) - 1
try:
 while True:
  number = random.randint(0, num)
  p = playlist[number]
  pygame.mixer.init()
  pygame.mixer.music.load(p)
  mc = mp3(p).info.length
  pygame.mixer.music.play(1)
  time.sleep(mc + 0.5)
except KeyboardInterrupt:
 sys.exit()

random.randint()を使って乱数を生成してそれをリストのインデックスに代入するとランダム再生が実現出来ます(完全ランダムなので何回も同じ曲が流れる可能性があります)
またこのプログラムはKeyboardInterruptエラー、つまりCtrl+Cキーでの強制終了によって終了します。

import pygame.mixer
import sys
import random
from mutagen.mp3 import MP3 as mp3
import time
playlist = ['/test/test1.mp3', '/test/test2.mp3', '/test/test3.mp3']
num = len(playlist) - 1
try:
 while True:
  numbers = random.randint(0, num)
  if numbers == number:
   continue
  number = numbers
  p = playlist[number]
  pygame.mixer.init()
  pygame.mixer.music.load(p)
  mc = mp3(p).info.length
  pygame.mixer.music.play(1)
  time.sleep(mc + 0.5)
except KeyboardInterrupt:
 sys.exit()

重複が嫌いな方はこちら

以下追記

上記のままだとif numbers == number:のところでNameErrorが出ると思います。
なので下のように最初にnumberに適当な値を入れてあげると動くと思います。

import pygame.mixer
import sys
import random
from mutagen.mp3 import MP3 as mp3
import time
number = 10 #ここは適当な値を入れてください
playlist = ['/test/test1.mp3', '/test/test2.mp3', '/test/test3.mp3']
num = len(playlist) - 1
try:
 while True:
  numbers = random.randint(0, num)
  if numbers == number:
   continue
  number = numbers
  p = playlist[number]
  pygame.mixer.init()
  pygame.mixer.music.load(p)
  mc = mp3(p).info.length
  pygame.mixer.music.play(1)
  time.sleep(mc + 0.5)
except KeyboardInterrupt:
 sys.exit()

方法2

import pygame.mixer
from mutagen.mp3 import MP3 as mp3
import time
import random
playlist = ['/test/test1.mp3', '/test/test2.mp3', '/test/test3.mp3']
random.shuffle(playlist)
for p in playlist:
    pygame.mixer.init()
    pygame.mixer.music.load(p)
    mc = mp3(p).info.length
    pygame.mixer.music.play(1)
    time.sleep(mc + 0.5)

この方法はコメント欄で教えていただきました。
リスト自体をシャッフルして再生をすることでランダム再生を実現するという手もあります。

import pygame.mixer
from mutagen.mp3 import MP3 as mp3
import time
import random
import sys

playlist = ['/test/test1.mp3', '/test/test2.mp3', '/test/test3.mp3']
class playmusic:
 def shuffle():
  random.shuffle(playlist)
  playmusic.play()
 def play():
  try:
   for p in playlist:
    pygame.mixer.init()
    pygame.mixer.music.load(p)
    mc = mp3(p).info.length
    pygame.mixer.music.play(1)
    time.sleep(mc + 0.5)
  except KeyboardInterrupt:
     sys.exit()
  playmusic.shuffle()

if __name__ == "__main__":
 playmusic.shuffle()

さっきのソースコードを元にして、全曲再生が終わると再びplaylistをシャッフルし再生するというプログラムを作ってみました

注意

この記事で紹介した内容は.mp3のみ対応しています(.m4a等では動かない)

終わり

連続再生などは、ファイルパスをリスト化してあげると実現するのが楽です

1
6
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
6