I'm very new to Python and I was trying to use a nice library (audiotools) to play an mp3 playlist, just as an exercise.
This is the class to play the tracklist (loosely based on THIS, once I discovered there is a "callback function with no arguments which is called by the player when the current track is finished" (*) ):
class Trackplay:
def __init__(self,
track_list,
audio_output=audiotools.player.open_output('ALSA'),
replay_gain=audiotools.player.RG_NO_REPLAYGAIN):
self.track_index = INDEX - 1
self.track_list = track_list
self.player = audiotools.player.Player(
audio_output,
replay_gain,
self.next_track())
def next_track(self):
try:
self.track_index += 1
current_track = self.track_list[self.track_index]
print str(current_track)
audio_file = audiotools.open(current_track)
self.player.open(audio_file) # <---------- error
self.player.play()
except IndexError:
print('playing finished')
Then I'm calling:
tp = Trackplay(get_track_list(PATH))
where get_track_list is a method returning a list of mp3s from the dir PATH.
The error I get (at the line marked with the "error" comment) is:
AttributeError: Trackplay instance has no attribute 'player'
I don't understand what's happening ...but reading all the AttributeError questions here, must be something stupid...
player seems to me exactly a Trackplay's attribute. Other attributes, as track_index and track_list seems OK, since the line print str(current_track) prints the current track.
Thanks for any help.
-
What is your OS ?Abdennour TOUMI– Abdennour TOUMI2017年03月24日 20:19:39 +00:00Commented Mar 24, 2017 at 20:19
-
Tried both on the Rapsberry Pi with Raspbian and the laptop with Linux Mint. Python version is 2.7.12dentex– dentex2017年03月24日 20:26:54 +00:00Commented Mar 24, 2017 at 20:26
1 Answer 1
See this code here?
self.player = audiotools.player.Player(
audio_output,
replay_gain,
self.next_track())
As part of creating the Player you're going to assign to self.player, you call self.next_track(). self.next_track tries to use self.player, before self.player exists!
def next_track(self):
try:
self.track_index += 1
current_track = self.track_list[self.track_index]
print str(current_track)
audio_file = audiotools.open(current_track)
self.player.open(audio_file)
self.player.play()
except IndexError:
print('playing finished')
next_track doesn't even return anything, so it's baffling why you're trying to pass self.next_track() as an argument to Player.
Was that supposed to be a callback? If so, you should pass self.next_track to Player without calling it.
self.player = audiotools.player.Player(
audio_output,
replay_gain,
self.next_track)
# ^ no call parentheses
1 Comment
self.next_track() with the parentheses (as a method) afterwards and it works... One song after the other... thank you sooo much. I understand what you say about the calling before the player even exists... now it's clear... I said it in the Q, noob error - well, actually two: the parentheses for the callback and the call before assignment - COOL. :)