Bottom Page

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()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to concatenate files while looping through lists? python_newbie09 3 387 Mar-24-2019, 03:11 PM
Last Post: python_newbie09
  Looping through csv files in a folder WhatsupSmiley 3 1,712 Nov-13-2018, 08:39 PM
Last Post: Larz60+
  Looping through files, check content and delete metalray 1 617 May-11-2018, 02:16 PM
Last Post: buran
  Looping .xlsx files in folder/subfolders copy pasting currentregion HarrisQ 4 2,230 Apr-17-2017, 06:35 AM
Last Post: HarrisQ

Forum Jump:


Users browsing this thread: 1 Guest(s)