首页 树莓派——音乐播放
文章
取消

树莓派——音乐播放

音乐播放

播放某个文件夹下面的音乐

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import pygame
def play_audio(audio_file_name):
    # Initialize pygame
    pygame.init()
    # Set the audio file
    pygame.mixer.music.load(audio_file_name)
    # Play the audio
    pygame.mixer.music.play()
    # Wait for the audio to finish
    while pygame.mixer.music.get_busy():
        pygame.time.Clock().tick(10)
    # Quit pygame
    pygame.quit()

def m_play_audio():
    # Directory for MP3 files
    directory = "/home/pi/Desktop/sleep/file/music/banzou"

    # Iterate through all files in the directory
    for filename in os.listdir(directory):
        if filename.endswith(".mp3"):
            file_path = os.path.join(directory, filename)
            play_audio(file_path)
本文由作者按照 CC BY 4.0 进行授权