简体   繁体   中英

How to play pause audio file in python?

I am trying to create a media player with Python that will play mp3 files one after the other and allow me to play and pause the music at any time (similar to spotify).

I have used the vlc library and pygame music function to play the files, but my problem comes when the song has finished and I want it to play the next file. I have manged to do this but not with a play and pause functionality.

My rough code:

import pygame
import time

#plays first mp3 file
file = '4c68Z9wLdHc36y3CNjwQKM.ogg'
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()

#play and pause funtionnality

while pygame.mixer.music.get_busy():
    timer = pygame.mixer.music.get_pos()
    time.sleep(1)
    control = input()
    pygame.time.Clock().tick(10)
    if control == "pause":
        pygame.mixer.music.pause()
    elif control == "play" :
        pygame.mixer.music.unpause()
    elif control == "time":
        timer = pygame.mixer.music.get_pos()
        timer = timer/1000
        print (str(timer))
    elif int(timer) > 10:
        print ("True")
        pygame.mixer.music.stop() 
        break
    else:
        pass

 #next mp3 file   

file = '3qiyyUfYe7CRYLucrPmulD.ogg' 
pygame.mixer.music.load(file)
pygame.mixer.music.play()

When I run this my hope is that it will play the first file and allow me to play and pause, but it stops when a song ends and not play the next one, as it gets stuck waiting for an input.

I want it to play the first file, allowing me to pause and resume it at any time, and then when the song has finished, it automatically plays the next file.

Thanks in advance.

To narrow it down I would like to know how to create a while that has a user input that will always check for a condition and not just at the start of the while loop.

I'm not sure why you tagged this question vlc if you are using pygame as arguably vlc.py has this functionallity pretty much built in.
However, all you need to do is use a double while statement.
The first controls the file to be played and the second performs your play/pause control. eg

import pygame
import time

files = ['./vp1.mp3','./vp.mp3']
pygame.init()
pygame.mixer.init()
stepper = 0
#file loading
while stepper < len(files):
    pygame.mixer.music.load(files[stepper])
    print("Playing:",files[stepper])
    stepper += 1
    pygame.mixer.music.play()
#play and pause
    while pygame.mixer.music.get_busy():
        timer = pygame.mixer.music.get_pos()
        time.sleep(1)
        control = input()
        pygame.time.Clock().tick(10)
        if control == "pause":
            pygame.mixer.music.pause()
        elif control == "play" :
            pygame.mixer.music.unpause()
        elif control == "time":
            timer = pygame.mixer.music.get_pos()
            timer = timer/1000
            print (str(timer))
        elif int(timer) > 10:
            print ("True")
            pygame.mixer.music.stop()
            break
        else:
            continue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM