Friday 31 May 2013

Tasker and XBMC, part 2: Create smart playlists and play them by using voice commands (UPDATED!)

Hello again everyone. I assume that you have read the previous post and will try to set up another magic trick with this one here. I will show you how to use your voice, talk to your phone and make XBMC create a playlist based on your command and actually start playing it.

Possible voice commands are for example: "Play music where album is Hairspray" or "Play music where genre is rock" or even "Play music where artist like Jason"
(Of course you need to have those in your music library, right?)

A prerequisite for all these to work is that your android phone and XBMC are connected to the same wireless network.



 The tools that you will be needing are more or less the same:

1. Tasker for android
2. SSH Plugin for Tasker
3. Autovoice

That should do it. I assume that you already have a raspberry Pi PC running XBMC Frodo (version 12). What we are going to do is store a script in there. This script must be stored in the folder /storage. I have named the script  createSmartPlaylist.sh, gave execute rights to it running the command

chmod +x createSmartPlaylist.sh 

and inserted the following lines of code in there:

 #!/bin/bash  
   
 if [ $# -ne 3 ]  
  then  
     echo ""  
     echo "Error: missing Arguments"  
   echo "3 arguments needed. First shall be field e.g. field='artist'"  
     echo "Second shall be operator e.g. operator='is'"  
     echo "Third shall be value e.g. <value>Alanis Morissette</value>"  
     exit 0  
 fi  
   
 echo " field = $1 "  
 echo " operator = $2 "  
 echo " value = $3 "  
   
 # $1 shall be field e.g. field="artist"  
 # $2 shall be operator e.g. operator="is"  
 # $3 shall be value e.g. <value>Alanis Morissette</value>  
   
 cd /storage/.xbmc/userdata/playlists/music  
   
 echo "<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>" > command.xsp  
 echo "<smartplaylist type="songs">" >> command.xsp  
 echo "  <name>smart command</name>" >> command.xsp  
 echo "  <match>all</match>" >> command.xsp  
 echo "  <rule field='"$1"' operator='"$2"'>" >> command.xsp  
 echo "    <value>"$3"</value>" >> command.xsp  
 echo "  </rule>" >> command.xsp  
 echo "</smartplaylist>" >> command.xsp
 exit 0
  

Basically, this script creates a file. The file is called command.xsp. The script needs 3 arguments to function and these 3 arguments are those that you will be passing through the autovoice plugin in Tasker.

To test the script, type in the XBMC terminal:
./createSmartPlaylist.sh artist is Nickelback

then run the following command:
cat /storage/.xbmc/userdata/playlists/music/command.xsp

and you should see the following:
<?xml version=1.0 encoding=UTF-8 standalone=yes ?>
<smartplaylist type=songs>
    <name>smart command</name>
    <match>all</match>
    <rule field='artist' operator='is'>
        <value>nickelback</value>
    </rule>
</smartplaylist>

So far so good. The script actually has created the smart playlist file and has passed the three arguments that we gave as input.

Go to your android phone and set up a new profile in Tasker. Use Events =>  Plugins => Autovoice recognized command. Configure the command with command filter "play music where" (without the quotes) and do not forget to check the event checkbox.




Next, we need to create the new task: Call it XBMC Party Mode and create a new command in there:
Plugins => SSH command launch.

Use your credentials to login to XBMC (username is root and password is openelec as default in the standard OPENELEC distribution). Then enter the following command in the command text:

/storage/./createSmartPlaylist.sh %avcommnofilter

your action should look like the pic on the right.













EDIT: I have been receiving some feedback that no matter that the smart playlist file gets updated, when XBMC starts playing, it plays the previous list. This is due to the fact that XBMC does not automatically update the content of smart playlists. To force an update we need to add this playlist to the current audio playlist.

To do this, insert here another action within your task:
Network => HTTP POST and enter the following in the Data / File tag:

 {"id":1,"jsonrpc":"2.0","method":"Playlist.Add","params":{"item":{"directory":"/storage/.xbmc/userdata/playlists/music/command.xsp"},"playlistid":0}}  


Server Port: your XBMC ip and port (in my case I entered 192.168.1.7 and no port because my port is the default port 80.
Path : /jsonrpc
Content-Type: application/json














 The next step is entering a new command under the same task: Network => HTTP POST and enter the following in the Data / File tag:

 {"jsonrpc":"2.0", "id":"1", "method":"Player.Open", "params":{ "item" :{ "partymode":"/storage/.xbmc/userdata/playlists/music/command.xsp"}}}  

Server Port: your XBMC ip and port (in my case I entered 192.168.1.7 and no port because my port is the default port 80.
Path : /jsonrpc
Content-Type: application/json

That´s all. Check the screenshots to make sure you got everything ok.

The final step is to bind the profile to an action of your choice. I have a simple profile that calls Autovoice recognize at the push of a shortcut on my android desktop.




Enjoy!

No comments:

Post a Comment