I have googled a bunch on how to fix this problem, but I'm kind of a beginner and none of the articles were related to python arcade, so I couldn't figure it out.
I'm trying to play background music in my game and I tried the python arcade Background Music example, but upon trying to get the position the example throws an error at me, saying: TypeError: get_stream_position() takes 1 positional argument but 2 were given
. Has some update messed up some code or something?
Heres the code I think is relevant to the problem:
def __init__(self, width, height, title):
super().__init__(width, height, title)
arcade.set_background_color(arcade.color.WHITE)
self.music_list = []
self.current_song_index = 0
self.current_player = None
self.music = None
def advance_song(self):
self.current_song_index += 1
if self.current_song_index >= len(self.music_list):
self.current_song_index = 0
print(f"Advancing song to {self.current_song_index}.")
def play_song(self):
if self.music:
self.music.stop()
print(f"Playing {self.music_list[self.current_song_index]}")
self.music = arcade.Sound(self.music_list[self.current_song_index], streaming=True)
self.current_player = self.music.play(MUSIC_VOLUME)
time.sleep(0.03)
def setup(self):
self.music_list = [":resources:music/funkyrobot.mp3", ":resources:music/1918.mp3"]
self.current_song_index = 0
self.play_song()
def on_update(self, dt):
position = self.music.get_stream_position(self.current_player)
if position == 0.0:
self.advance_song()
self.play_song()
Here's the full error:
Playing :resources:music/funkyrobot.mp3
Traceback (most recent call last):
File "c:/Users/[name]/Documents/Projects/background_music.py", line 77, in <module>
main()
File "c:/Users/[name]/Documents/Projects/background_music.py", line 74, in main
arcade.run()
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\arcade\window_commands.py", line 236, in run
pyglet.app.run()
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\app\__init__.py", line 107, in run
event_loop.run()
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\app\base.py", line 167, in run
timeout = self.idle()
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\app\base.py", line 237, in idle
redraw_all = self.clock.call_scheduled_functions(dt)
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\clock.py", line 292, in call_scheduled_functions
item.func(now - item.last_ts, *item.args, **item.kwargs)
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\arcade\application.py", line 213, in _dispatch_updates
self.dispatch_event('on_update', delta_time)
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\window\__init__.py", line 1333, in dispatch_event
if EventDispatcher.dispatch_event(self, *args) != False:
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\event.py", line 422, in dispatch_event
self._raise_dispatch_exception(event_type, args, getattr(self, event_type), exception)
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\event.py", line 476, in _raise_dispatch_exception
raise exception
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\event.py", line 415, in dispatch_event
if getattr(self, event_type)(*args):
File "c:/Users/[name]/Documents/Projects/background_music.py", line 65, in on_update
position = self.music.get_stream_position(self.current_player)
TypeError: get_stream_position() takes 1 positional argument but 2 were given
TL;DR: Example I wanted to use as reference doesn't function - TypeError: get_stream_position() takes 1 positional argument but 2 were given
.
Seems, that you're using old Arcade version. You have two options:
pip install arcade --upgrade
self.music.get_stream_position()
Here is the working example for old Arcade version: