Python Forum
Looping through music files (SOLVED)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping through music files (SOLVED)
#1
Hi,

I just cannot understand why the below code is not looping.

It plays a song and then stops and exits.

TIA

#!/usr/bin/python3

import random, os
import pygame

#music files path
path = "/media/usb/"

#get music files
songs = os.listdir(path)

#filter mp3 files
songs = [fi for fi in songs if fi.endswith(".mp3") ]

pygame.init()
pygame.mixer.init()

while True:
    try:
        #pygame.mixer.music.set_volume(0.50)
        filename = random.choice(songs)
        print('playing now {}'.format(filename))
        pygame.mixer.music.load(path + filename)
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy():
            pygame.time.Clock().tick(10)
    except ValueError:
        print('Excemption: {}', format(ValueError))

#if __name__ == '__main__':
EDIT:
Solved by moving lines around ;)

#!/usr/bin/python3

import random, os
import pygame

#music files path
path = "/media/usb/"

#get music files
songs = os.listdir(path)

#filter mp3 files
songs = [fi for fi in songs if fi.endswith(".mp3") ]

pygame.init()
pygame.mixer.init()
#print(pygame.mixer.get_init())

def play_songs():
    try:
        #pygame.mixer.music.set_volume(0.50)
        filename = random.choice(songs)
        #print('playing now {}'.format(filename))
        pygame.mixer.music.load(path + filename)
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy():
            pygame.time.Clock().tick(10)
    except ValueError:
        print('Excemption: {}', format(ValueError))

if __name__ == '__main__':
    while True:
        play_songs()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [solved] compressing files with python. SpongeB0B 1 648 May-26-2023, 03:33 PM
Last Post: SpongeB0B
  Help replacing word in Mutiple files. (SOLVED) mm309d 0 834 Mar-21-2023, 03:43 AM
Last Post: mm309d
  Delete empty text files [SOLVED] AlphaInc 5 1,567 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  Sorting and Merging text-files [SOLVED] AlphaInc 10 4,890 Aug-20-2021, 05:42 PM
Last Post: snippsat
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,132 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
Question (solved) open multiple libre office files in libre office lucky67 5 3,322 May-29-2021, 04:54 PM
Last Post: lucky67
  Looping through Folder structure and get files mfkzolo 0 1,892 Nov-02-2020, 08:31 AM
Last Post: mfkzolo
  How to concatenate files while looping through lists? python_newbie09 3 2,872 Mar-24-2019, 03:11 PM
Last Post: python_newbie09
  Looping through csv files in a folder WhatsupSmiley 3 9,065 Nov-13-2018, 08:39 PM
Last Post: Larz60+
  Looping through files, check content and delete metalray 1 2,386 May-11-2018, 02:16 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020