rablentain
rablentain

Reputation: 6725

Playing music with pyspotify, what am I missing?

Im new to Python and I am trying to make a class where I will play some music with the spotify library pyspotify. I have the code below and I taught that it would be playing music if I run the play method. This does not work because I can't hear any music playing, no error messages appear from what I can see. What do I have to do more?

import spotify
import threading

class Music:
    session = None

    def __init__(self):
        logged_in_event = threading.Event()

        def connection_state_listener(session):
            if session.connection.state is spotify.ConnectionState.LOGGED_IN:
                logged_in_event.set()

        self.session = spotify.Session()
        loop = spotify.EventLoop(self.session)
        loop.start()
        self.session.on(
                   spotify.SessionEvent.CONNECTION_STATE_UPDATED,
                   connection_state_listener)

        self.session.login('accountname', 'password')
        logged_in_event.wait()

        print self.session.connection.state
        print self.session.user

    def play(self):
        track = self.session.get_track('spotify:track:2Foc5Q5nqNiosCNqttzHof')
        track.load()

        self.session.player.load(track)
        self.session.player.play(play=True)

And in another Python file I do:

music = Music.Music()
music.play()

Upvotes: 0

Views: 1776

Answers (1)

rablentain
rablentain

Reputation: 6725

I added the line

audio = spotify.AlsaSink(session)

and now it works!

Upvotes: 1

Related Questions