Background Music

Screen shot of a background music example
background_music.py
  1"""
  2Background Music Example
  3
  4If Python and Arcade are installed, this example can be run from the command line with:
  5python -m arcade.examples.background_music
  6"""
  7import arcade
  8import time
  9
 10SCREEN_WIDTH = 600
 11SCREEN_HEIGHT = 300
 12SCREEN_TITLE = "Starting Template Simple"
 13MUSIC_VOLUME = 0.5
 14
 15
 16class MyGame(arcade.Window):
 17    """ Main application class. """
 18
 19    def __init__(self, width, height, title):
 20        super().__init__(width, height, title)
 21
 22        arcade.set_background_color(arcade.color.WHITE)
 23
 24        # Variables used to manage our music. See setup() for giving them
 25        # values.
 26        self.music_list = []
 27        self.current_song_index = 0
 28        self.current_player = None
 29        self.music = None
 30
 31    def advance_song(self):
 32        """ Advance our pointer to the next song. This does NOT start the song. """
 33        self.current_song_index += 1
 34        if self.current_song_index >= len(self.music_list):
 35            self.current_song_index = 0
 36        print(f"Advancing song to {self.current_song_index}.")
 37
 38    def play_song(self):
 39        """ Play the song. """
 40        # Stop what is currently playing.
 41        if self.music:
 42            self.music.stop()
 43
 44        # Play the next song
 45        print(f"Playing {self.music_list[self.current_song_index]}")
 46        self.music = arcade.Sound(self.music_list[self.current_song_index], streaming=True)
 47        self.current_player = self.music.play(MUSIC_VOLUME)
 48        # This is a quick delay. If we don't do this, our elapsed time is 0.0
 49        # and on_update will think the music is over and advance us to the next
 50        # song before starting this one.
 51        time.sleep(0.03)
 52
 53    def setup(self):
 54        """ Set up the game here. Call this function to restart the game. """
 55
 56        # List of music
 57        self.music_list = [":resources:music/funkyrobot.mp3", ":resources:music/1918.mp3"]
 58        # Array index of what to play
 59        self.current_song_index = 0
 60        # Play the song
 61        self.play_song()
 62
 63    def on_draw(self):
 64        """ Render the screen. """
 65
 66        arcade.start_render()
 67
 68        position = self.music.get_stream_position(self.current_player)
 69        length = self.music.get_length()
 70
 71        size = 20
 72        margin = size * .5
 73
 74        # Print time elapsed and total
 75        y = SCREEN_HEIGHT - (size + margin)
 76        text = f"{int(position) // 60}:{int(position) % 60:02} of {int(length) // 60}:{int(length) % 60:02}"
 77        arcade.draw_text(text, 0, y, arcade.csscolor.BLACK, size)
 78
 79        # Print current song
 80        y -= size + margin
 81        text = f"Currently playing: {self.music_list[self.current_song_index]}"
 82        arcade.draw_text(text, 0, y, arcade.csscolor.BLACK, size)
 83
 84    def on_update(self, dt):
 85
 86        position = self.music.get_stream_position(self.current_player)
 87
 88        # The position pointer is reset to 0 right after we finish the song.
 89        # This makes it very difficult to figure out if we just started playing
 90        # or if we are doing playing.
 91        if position == 0.0:
 92            self.advance_song()
 93            self.play_song()
 94
 95
 96def main():
 97    """ Main method """
 98    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 99    window.setup()
100    arcade.run()
101
102
103if __name__ == "__main__":
104    main()