0

I have a collection of songs and I'd like to have them played in sequence.

If I run the function below, it will play milliseconds of each song but only the last one on the list in its entirety.

def play(*args):
    for id_ in args:
        print 'playing', id_
        os.system("osascript -e 'tell application \"Spotify\" to play track \"%s\"'" % (id_,))

what can I add to the function in order to tell the system to play all song ids (the args) for an ammount of time t?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198
  • 1
    Use [`subprocess`](https://docs.python.org/3.5/library/subprocess.html) instead of `os.system`. That way you can wait for the process to complete before looping again. See [here](http://stackoverflow.com/questions/30470759/python-subprocess-kill-process-after-timed-delay) – Paul Rooney May 03 '16 at 04:22

1 Answers1

0

Solution

import time

def play(t=1, *args):
    for id_ in args:
        print 'playing', id_
        os.system("osascript -e 'tell application \"Spotify\" to play track \"%s\"'" % (id_,))
        time.sleep(t)

Explanation

time.sleep(t) sleeps for t seconds.

piRSquared
  • 285,575
  • 57
  • 475
  • 624