1
我有這個function
,發揮Spotify
:如何設置一個Timer來控制循環內的函數調用?
#a function to play Spotify
def play(id_):
print 'playing', id_
os.system("osascript -e 'tell application \"Spotify\" to play track \"%s\"'" % (id_,))
及以下loop
,通過所有playlist
歌曲,其迭代,獲得所有可播放id
的(foreign_id
),將它們傳遞給play(id_)
,
和將每首歌曲duration
轉換爲time.sleep()
以停止循環,直到每首歌曲結束爲止,重複循環:
for i, song in enumerate(song_playlist):
#we need to track each song id
song_id = song_playlist[i]['id']
#in order to get song 'duration', access 'song/profile response' and pass the id as an argument
response_profile = en.get('song/profile', id=song_id, bucket="audio_summary")
song_profile = response_profile['songs']
dur = song_profile[0]['audio_summary']['duration']
#convert to miliseconds
dur *= 1000
print int(round(dur))
#now we access each song 'foreign_id'
for track in song:
track = song['tracks'][i]
track_id = track['foreign_id'].replace('-WW', '')
print '{0} {2} {1}'.format(i, song['artist_name'], song['title'])
#call the function for each track
play(track_id) #CALL FUNCTION HERE
time.sleep(int(round(dur))) # SET INTERVAL CALL TO EACH SONG DURATION
然而,只有一首歌曲播放,遞歸消失。
我該如何更正代碼,以便我可以使用功能按順序播放所有曲目,只能運行一次代碼?
不能'song_id'只能通過'song_id = song ['id']得到' – Natecat