Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- from pathlib import Path
- from pygame import mixer
- class Player:
- def __init__(self):
- self.sounds = self._setup()
- self.channel = None
- def _setup(self):
- mixer.init()
- return [
- mixer.Sound(file)
- for file in Path(r"C:\Windows\Media").glob("*.wav")
- if file.is_file()
- ]
- def __len__(self):
- return len(self.sounds)
- def stop(self):
- if self.channel is not None:
- self.channel.stop()
- self.channel = None
- def play(self, index):
- if 0 <= index < len(self):
- self.stop()
- self.channel = self.sounds[index]
- self.channel.play()
- def main():
- player = Player()
- while True:
- input("Enter to play a sound ")
- player.play(randint(0, len(player) - 1))
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement