I am a music lover. So I listen to musics a lot. I not only listen to music, but also share it. For this purpose I made a php script long time ago that is used for song broadcasting. In windows I used winmap’s “dosomething” plugin to broadcast. Nowadays I listen song in my linux machine. No winamp is there. I use amarok to listen song. Amarok is a great player if you ever used it, you’ll realize. But I couldn’t broadcast song using amarok. Being a programmer I didnt search any plugin or script, rather I thought I’d write it myself.
I googled for writing amarok plugin. And I found these are not plugin actually. These are called script. Amarokscript. So I have to write amarok script. In the amarok wiki all the example was in Ruby and Python. But they said, any language can be used even bash would do. So I though why not try it with PHP. As I write PHP a lot. So It’ll be faster.
The amarok wiki in kde.org helped me a lot. Actually I have realy only that page and start writing the script. Bellow goes the main howto.
An amarokscriopt is an tar archive file. It contians.
script.php (executable script)
README: When you click on the about button/tab for this script in amarok the contents of this file is shown.
COPYING: If you have this file a tab/button named License will be shown. And the text from there will be contained there.
script.php: its the main script. It MUST be executable. In example I used .php extension. It can be any extension. For Ruby its .rb, for python its .py, for perl its .pl, for binary it can be empty or .o. the main point is it MUST be executable.
script.spec: Its the file where you’ll write configureation values for amarok. Amarok will read this file and determine which file is the main script and what type of script is this. In spec file there are 2 main entries. Name and type. If name is not given the first part of the .spec file is used as name. And type is a must.
The main script for me was in PHP. The main script should be executable. So I change the mode of the script to executable.
chmod +x songinfo-broadcaster.php
but even its executable, it can not run. Because shell doesn’t know what interpreter to use to run it. From shell you have to run it by,
$ php songinfo-broadcaster.php
Amarok wont call it that way for sure. If it would call it that way it wouldn’t require the script to be executable. It will be call like,
To make sure that this script runs this way add the following line at the top of the script.
The script will look like,
Now I have to code in between the php tags.
Here goes my main script skeleton. The dowork() function does the main script logic.
echo "starting script", PHP_EOL;
echo "starting listenning events", PHP_EOL;
$fp = fopen("php://stdin","r");
while($command = fgets($fp)):
echo "Event: $command",PHP_EOL;
$com = trim($command);
echo "event to procsss: $com", PHP_EOL;
echo "Script Exited", PHP_EOL;
My script should execute the main logic only when the trackChange event occurred. So I had to catch it. Amarok sends event information in the scripts STDIN. So I read the STDIN. And when its trackChange I call the dowork(). Very simple logic.
I also needed current song information. It can be collected by DCOP api. DCOP is desktop communication protocol. While running amarok issue the following command in consoe,
$ dcop amarok player title
You’ll see the current songs title.
This technique is used to get the current song information. My dowork function is,
$length = `dcop amarok player trackTotalTime`;
echo "Length: $length",PHP_EOL;
$album =`dcop amarok player album`;
echo "Album: $album",PHP_EOL;
$title =`dcop amarok player title`;
echo "Title: $title",PHP_EOL;
$artist = `dcop amarok player artist`;
echo "Artist: $artist",PHP_EOL;
$sec = $length % 60;
$min = (int)(($length-$sec)/60);
$length = "$min:$sec";
// Code to broadcast length, artist, album and title
See, how did I collect those information. I just used the backtick operator in php to capture shell command output.
Main script is done. Now the spec file needs to be writen.
I just put the following line in songinfo-broadcaster.spec file.
type = generic
After that I wrote some notes about the script in README. Then a copyright notice in COPYING.
When every thing is written. Its time to package it. My directory structure was,
I pack it by following command.
$ tar -cf songinfo-broadcaster.amarokscript.tar -p songinfo-broadcaster
One this I MUST say. Every amarok script archive must be a .amarokscript fie. That means when they are in tar archive it will be .amarokscriopt.tar extension and when they are in bz2 archive it will be .amarokscript.tar.bz2.
The script is created.
2)Go to Tools->ScriptManger.
3)Click Install Script button
4)Browse the script and load it
5)Select the script and click RUN
The script will be running.