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

(singke) #1

drive.


LISTING 23.5 The script2305.py Special Presentation


Click here to view code image


#script2305.py - Special HD Presentation with Sound
#Written by Blum and Bresnahan
#
##########################################
#
##### Import Functions & Variables #######
#
from os import listdir, system #Import from OS module
#
#Import from PyGame Library
from pygame import event, font, display, image, init, mixer, time, transform
#
from random import randint #Import from Random module
#
from sys import exit #Import from System module
#
from pygame.locals import * #Load PyGame constants
#
init() #Initialize PyGame
#
# Load Music Play List Function ##################
#
# Read Playlist and Queue up Songs #
#
def Load_Music ():
#
global SongList #Make SongList global
SongList = [] #Initialize SongList to Null
#
global SongNumber #Make SongNumber global
SongNumber = 0 #Initialize Song Number to 0
#
global MaxSongs #Make MaxSongs global
MaxSongs = 0 #Initialize Maximum Songs to 0
#
PlayList = PictureDirectory + '/' + '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 = PictureDirectory + '/' + 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
Free download pdf