我是Python的新手,我正在尝试创建一个类,我将使用spotify库pyspotify播放一些音乐。我有下面的代码,我教过如果我运行播放方法,它将播放音乐。这不起作用,因为我无法听到任何音乐播放,我看不到任何错误消息。我还需要做些什么?
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)
在我做的另一个Python文件中:
music = Music.Music()
music.play()
答案 0 :(得分:1)
我添加了
行audio = spotify.AlsaSink(session)
现在它有效!