0

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.

asked Mar 24, 2017 at 20:19
2
  • What is your OS ? Commented 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.12 Commented Mar 24, 2017 at 20:26

1 Answer 1

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
answered Mar 24, 2017 at 20:25
Sign up to request clarification or add additional context in comments.

1 Comment

BINGO! I removed the parentheses and then called a 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. :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.