Package speakeasy :: Module music_player :: Class MusicPlayer
[hide private]
[frames] | no frames]

Class MusicPlayer

source code

object --+
         |
        MusicPlayer

Plays music files, currently ogg and wav. In contrast to SoundPlayer, which is optimized for dealing with lots of short sounds, this facility is for longer files, which are streamed, rather than loaded. Also in contrast to SoundPlayer, MusicPlayer can only play one song at a time.

For ogg files the method setPlayhead() allows clients to move foward and back within a song as it plays.

Public methods:

  1. play()
  2. pause()
  3. unpause()
  4. setSoundVolume()
  5. getSoundVolume()
  6. setPlayhead()
  7. getPlayheadPosition()
  8. getPlayStatus()

    Requires pygame.

Instance Methods [hide private]
 
__init__(self)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
play(self, whatToPlay, repeats=0, startTime=0.0, blockTillDone=False, volume=None)
Play an .mp3 or .ogg file, or File class instance.
source code
 
stop(self)
Stop music if any is playing.
source code
 
pause(self)
Pause either currently playing song, if any.
source code
 
unpause(self)
Unpause a paused song.
source code
 
setSoundVolume(self, volume)
Set sound playback volume.
source code
float
getSoundVolume(self)
Get currently set sound volume.
source code
 
setPlayhead(self, secs, timeReference=1)
Set playhead to 'secs' seconds into the currently playing song.
source code
float
getPlayheadPosition(self)
Return number of (possibly fractional) seconds to where the current song is currently playing.
source code
 
getPlayStatus(self)
Return one of PlayStatus.STOPPED, PlayStatus.PLAYING, PlayStatus.PAUSED to reflect what the player is currently doing.
source code
boolean
waitForSongDone(self, timeout=None)
Block until song is done playing.
source code
boolean
formatSupported(self, fileExtension)
Checks whether the given file extension implies a supported sound format.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
  singletonInstanceRunning = False
  supportedFormats = ['ogg', 'wav']
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__
(inherited documentation)

play(self, whatToPlay, repeats=0, startTime=0.0, blockTillDone=False, volume=None)

source code 

Play an .mp3 or .ogg file, or File class instance. Note that pygame does not support startTime for .wav files. They will play, but startTime is ignored. Offers choice of blocking return until the music is finished, or returning immediately.

Parameters:
  • whatToPlay ({string | File}) - Full path to .wav or .ogg file, or File instance.
  • repeats (int) - Number of times to repeat song after the first time. If -1: repeat forever, or until another song is played.
  • startTime (float) - Time in seconds into the song to start the playback.
  • blockTillDone (boolean) - True to delay return until music is done playing.
  • volume (float) - How loudly to play (0.0 to 1.0). None: current volume.
Raises:
  • IOError - if given music file path does not exist, or some other playback error occurred.
  • ValueError - if given volume is not between 0.0 and 1.0
  • TypeError - if whatToPlay is not a filename (string), or Sound instance.
  • NotImplementedError - if startTime is other than 0.0, but the underlying music engine does not support start time control.

setSoundVolume(self, volume)

source code 

Set sound playback volume.

Parameters:
  • volume (float) - Value between 0.0 and 1.0.
Raises:
  • TypeError - if volume is not a float.
  • ValueError - if volume is not between 0.0 and 1.0

getSoundVolume(self)

source code 

Get currently set sound volume.

Returns: float
Volume number between 0.0 and 1.0

setPlayhead(self, secs, timeReference=1)

source code 

Set playhead to 'secs' seconds into the currently playing song. If nothing is being played, this method has no effect. If a song is currently paused, the song will be unpaused, continuing to play at the new playhead position.

Parameters:
  • secs (float) - number of (possibly fractional) seconds to start into the song.
  • timeReference (TimeReference) - whether to interpret the secs parameter as absolute from the song start, or relative from current position. Options are TimeRerence.ABSOLUTE and TimeRerence.RELATIVE
Raises:
  • NotImplementedError - if called while playing song whose format does not support playhead setting in pygame.

getPlayheadPosition(self)

source code 

Return number of (possibly fractional) seconds to where the current song is currently playing. If currently playing nothing, return 0.0.

Returns: float
number of fractional seconds where virtual playhead is positioned.

waitForSongDone(self, timeout=None)

source code 

Block until song is done playing. Used in play() method.

Parameters:
  • timeout ({int | float}) - Maximum time to wait in seconds.
Returns: boolean
True if song ended, False if timeout occurred.

formatSupported(self, fileExtension)

source code 

Checks whether the given file extension implies a supported sound format.

Parameters:
  • fileExtension (string) - file extension with or without leading period. Example: ".ogg"
Returns: boolean
True if the format is supported, else False.
Raises:
  • ValueError - if fileExtension is anything other than a string with length > 0.