微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何让 pygames 播放文件夹中的音频?

如何解决如何让 pygames 播放文件夹中的音频?

你好,我是编程新手,我想做一个歌曲播放器。

import os
from pygame import mixer
Path = input("please provide path to folder: ")
os.chdir(Path)
mixer.init()
mixer.music.load.(Path)

这是我目前的代码

解决方法

这里我指定了包含所有歌曲的文件夹,您只需要输入特定的歌曲名称即可。(在给出路径时始终使用“\”)

import pygame
pygame.mixer.init()
song=str(input('Song Name: ')) #ask for song name
path='C:\\folder_name\\'+song 

pygame.mixer.music.load(path)
pygame.mixer.music.play(loops=3) #how many times to repeat the song
,

您需要确保程序没有退出。所以添加一个 while 循环。希望这个答案对您有所帮助。

from pygame import mixer
Path = input("please provide path to folder: ")
mixer.init()
mixer.music.load(Path)
# Setting the volume 
mixer.music.set_volume(0.7) 

# Start playing the song 
mixer.music.play() 
# infinite loop 
while True: 
  
    print("Press 'p' to pause,'r' to resume") 
    print("Press 'e' to exit the program") 
    query = input("  ") 
  
    if query == 'p': 
        # Pausing the music 
        mixer.music.pause()      
    elif query == 'r': 

        # Resuming the music 
        mixer.music.unpause() 
    elif query == 'e': 

        # Stop the mixer 
        mixer.music.stop() 
        break

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。