Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

PlayList = MusicDirectory + '/' + 'playlist.txt'
PlayList = open(PlayList, 'r')



for Song in PlayList: #Load PlayList into SongList



Song = Song.rstrip('\n') #Strip off newline
if Song != "": #Avoid blank lines in PlayList
Song = MusicDirectory + '/' + Song
SongList.append(Song)
MaxSongs = MaxSongs + 1
PlayList.close()



Play The Music Function



def Play_Music (SongList,SongNumber):
mixer.music.load(SongList[SongNumber])
mixer.music.play(0)
mixer.music.set_endevent(USEREVENT) #Send event when Music Stops



Gracefully Exit Script Function



def Graceful_Exit ():
mixer.music.stop() #Stop any music.
mixer.quit() #Quit mixer
time.delay(3000) #Allow things to shutdown
Command = "sudo umount " + MusicDisk
system(Command) #Unmount disk
exit()



Set up Music Variables



MusicDirectory = '/home/pi/music'
MusicDisk = '/dev/sda1'



Mount the Music Drive



Command = "sudo umount " + MusicDisk + " 2>/dev/null"
system(Command)
Command = "sudo mount -t vfat " + MusicDisk + " " + MusicDirectory
system(Command)



Queue up the Music



Load_Music()
Play_Music(SongList,SongNumber)
SongNumber = SongNumber + 1



#Set up Display for Event Handling ##############



MusicScreen = display.set_mode((0,0))
display.set_caption("Playing Music...")



while True: #Keep playing the Music ############
for Event in event.get():



if Event.type == USEREVENT:
if SongNumber < MaxSongs:
Play_Music(SongList,SongNumber)
SongNumber = SongNumber + 1
if SongNumber >= MaxSongs:
SongNumber = 0 #Start over in PlayList

Free download pdf