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
  Looping JSON data graham23s 1 99 Jul-01-2019, 09:37 PM
Last Post: nilamo
  looping and indentation issue ameydiwanji 3 119 Jul-01-2019, 10:53 AM
Last Post: perfringo
  looping for time while skipping a day Staph 2 145 Jun-23-2019, 03:33 AM
Last Post: Staph
  Random access binary files with mmap - drastically slows with big files danart 1 172 Jun-17-2019, 10:45 AM
Last Post: danart
  How to keep looping until the user input is valid ? KyawMyo 12 1,032 Jun-10-2019, 02:51 AM
Last Post: KyawMyo
  Need help looping this program JakobeTheKid 1 151 May-19-2019, 05:30 AM
Last Post: SheeppOSU
  Delete Lines that Contain Words - Loop through files in a folder - Write to new files dj99 3 297 May-18-2019, 06:34 AM
Last Post: heiner55
  Looping a function wendysling 3 188 May-09-2019, 09:53 PM
Last Post: wendysling
  How to concatenate files while looping through lists? python_newbie09 3 267 Mar-24-2019, 03:11 PM
Last Post: python_newbie09
  Looping through an if/else statement tle003 1 169 Mar-21-2019, 06:25 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)