使下一首歌播放自动化

时间:2016-10-14 13:28:59

标签: python-2.7 pygame

我正在使用PyQt制作GUI音乐播放器,我几乎已经完成了当我播放完音乐时,下一首音乐不会自动播放,所以怎么做,这就是代码

def playMusic():
    global counter,music
    counter += 1
    pygame.mixer.init()
    pygame.mixer.music.load(music_folder + "/" + music[counter])
    pygame.mixer.music.play()
    print 'playing ' + music[counter]

1 个答案:

答案 0 :(得分:0)

我对PyQt没有太多经验,但如果你知道音乐有多长,那就做这样的事情:

import time  # Type this somewhere near the top of your program
  .
  .
  .       
x = 9001  # define this variable somewhere, it should be the length of your song in seconds
  .
  .
  .
while True: # infinite loop

    if not playing:  # if the song isn't already playing, 
        playMusic()  # start the song
        playing = True

    else:
        time.Sleep(x)     
        playing = False

如果您希望能够在播放时暂停播放歌曲,则可能需要将playMusic()函数更改为以下内容:

def playMusic(counter):    # add counter argument, remove "counter += 1"
    global counter,music
    pygame.mixer.init() 
    pygame.mixer.music.load(music_folder + "/" + music[counter])
    pygame.mixer.music.play()
    print 'playing ' + music[counter]

然后在函数外添加计数器变量。将你的歌曲长度(以秒为单位)放在循环上方某处的x变量中:

x = (9001, 21, 1337, 711)
  .
  .
  .
while not paused: # stop if it's paused

    if playing:  
        time.Sleep(1) 
        songtime += 1      # if the song is playing, count the seconds until it's over
        if songtime >= x[counter]:
            playing = False

    else:
        playMusic(counter)       # start the song if it's not playing
        playing = True 
        counter += 1