how to play itunes music's from terminal or aiml pattern

I want to write a command that launches iTunes and play the first song that is in the playlist or random song ,for now I can only launch iTunes from my code but it doesn't start the song automatically, I added --play but its doesn't work ..it there away to make it work? my aiml pattern is here 

```<category> <pattern> PLAY MUSIC FROM MY COMPUTER </pattern> <template> <random> <li>Sure thing! </li> <li>OKAY, </li> <li>OK! </li> </random> <system> open -a /Applications/itunes.app/Contents/MacOS/itunes/play</system> </template> </category>```

hairygael's picture

Hello,You can do it this way

Hello,

You can do it this way in the AIML:

<category><pattern>PLAY MUSIC</pattern>
<template><random>
        <li>Sure thing!</li>
        <li>Playing music now</li>
        <li>OK!</li>
      </random>
    <oob><mrl><service>python</service><method>exec</method><param>play()</param></mrl></oob></template>
</category>
<category><pattern>STOP MUSIC</pattern>
<template><random>
        <li>As you please</li>
        <li>Stopping music now</li>
        <li>OK!</li>
      </random>
    <oob><mrl><service>python</service><method>exec</method><param>stop()</param></mrl></oob></template>
</category>
<category><pattern>PAUSE MUSIC</pattern>
<template><random>
        <li>Music in pause</li>
        <li>Pausing music now</li>
        <li>OK!</li>
      </random>
    <oob><mrl><service>python</service><method>exec</method><param>pause()</param></mrl></oob></template>
</category>
<category><pattern>RESUME MUSIC</pattern>
<template><random>
        <li>Resuming music now</li>
        <li>Sure!</li>
      </random>
    <oob><mrl><service>python</service><method>exec</method><param>resume()</param></mrl></oob>
</template>

And then you add a python script for each command. First one includes the path to your music directory:

import random

def play():
    global musiconoff
    musiconoff = 1
    musicpath = C:\Users\tommy\itunes\Media\
    files = os.listdir(musicpath)
    song=random.choice(files)
    sleep(3)
    #i01.mouth.speakBlocking("playing song" + str(song))
    AudioPlayer.playFile(musicpath + str(song) , False)
    print("playing song:" + str(song))
    sleep(1)
    ear.startListening()
    ear.setAutoListen(True)

# pause method
def pause():
    if musiconoff == 1:
      AudioPlayer.pause()
      print "set music on pause"

# resume method
def resume():
    if musiconoff == 1:
      AudioPlayer.resume()
      print "resuming music"
       
# stopped method is called when at the end of an audio file
def stop():
    if musiconoff == 1:
        AudioPlayer.stop()
        print "stop playing"

The issue with this, is that it won't open or select music in subdirectories.

I don't have the time to dig further, but this gives you already a way to make it work it out.

I have just added this to the InMoov develop and it works nicely.