Spotify Connect

Hi,

a few days ago I asked for a possibility to stop the spotify player without killing and restarting the whole process!

In the meantime I’ve investigated a bit time and studied the code and concept of the python script ‘main.py’ . After a while I found a way to retrieve status and metadata from the spotify client by using a small self written python script. Here are some examples how this sript works:

spconnect_ctrl command [parameter]

pi@P1volumio:~$ ./spconnect_ctrl status
active : False
logged_in : True
repeat : False
shuffle : False
playing : False

pi@P1volumio:~$ ./spconnect_ctrl  status  active
False

pi@P1volumio:~$ ./spconnect_ctrl metadata
volume : 52428
context_uri : spotify:user:firstname.lastname:playlist:4gt54xczecpEK5C4k9S
cover_uri : spotify:image:87779c10f715c6f2afeb04c2d5619b933fc18c58
artist_name : Madrugada
track_uri : spotify:track:4C201yNPCEnMbTMO1yP5mR
artist_uri : spotify:artist:0iC8O5ABswVUFiYwM94bu3
track_name : Quite Emotional - Live; 2010 Remastered Version
album_uri : spotify:album:5zylhCTCHQstRpSU47BEiw
duration : 258934
album_name : The Best Of Madrugada
data0 : Best of Bests

pi@P1volumio:~$ ./spconnect_ctrl pause
OK

For those who are interested in the code:

[code]#!/usr/bin/python

-- coding: utf-8 --

Get status from Spotify Connect Client on Volumio

Creation: 29.02.2016

Last Update: 29.02.2016

Copyright © 2016 by Mr.Sponti

This program is free software; you can redistribute it and/or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation; either version 2 of the License, or

(at your option) any later version.

import required modules

import os
import sys
import requests
import json

sp_config = {
‘client’ : os.uname()[1],
‘port’ : ‘4000’,
‘status’ : (’/api/info/status’, ‘i’),
‘metadata’: (’/api/info/metadata’, ‘i’),
‘pause’ : (’/api/playback/pause’,‘p’),
‘play’ : (’/api/playback/play’, ‘p’)
}

def main ():

get command line parameter

if len(sys.argv) == 1:
print (“usage: %s command [parameter]” % sys.argv[0])
sys.exit()
if len(sys.argv) >= 2:
command = sys.argv[1]
if len(sys.argv) >= 3:
parameter = sys.argv[2]
else :
parameter = None

send http request to get data from spotify client

try:
rdata = requests.get(‘http://’+ sp_config[‘client’] + ‘:’ + sp_config[‘port’] + sp_config[command][0])
if rdata.ok :
if sp_config[command][1] == ‘p’ :
print (‘OK’)
else :
if not parameter :
for parameter,pvalue in rdata.json().items():
print (’%s : %s’ % (parameter, pvalue))
else :
print rdata.json()[parameter]
else :
print (‘Error: %s’ % rdata.status_code)

except requests.exceptions.ConnectionError as error:
print (‘Connection Error: %s’ % error)
except requests.exceptions.HTTPError as error:
print (‘HTTP Error: %s’ % error)

if name == ‘main’:

main()[/code]

after creating the file you need to change the ownership and permission.

chmod 755 spconnect_ctrl
sudo chown root  spconnect_ctrl
sudo chgrp root spconnect_ctrl
pi@P1volumio:~$ ls -l spconnect_ctrl
-rwxr-xr-x  1 root root  1823 Feb 29 17:04 spconnect_ctrl

In case that someone is planning to build a player with a lcd, the sript can be used to read the metadata from the spotify client.

Any updates on this? Pretty important feature to get the family on board.

What do you mean by killing the whole process? You can stop playing Spotify Connect by selecting another device in the Spotify app.

My question in regard to stop the spotify player was related to control spotify from PI’s shell level and not from a remote spotify client.
I’m currently building a network player based on volumio and in case that the device is switched into standby mode by a button or a IR receiver connected to the GPIO pins, I would like to stop the current running player (mpd or spotify).

In the meantime I’ve developed a solution by myself. I’m now able to retrieve the actual status of the spotify client and also to send a stop command from shell level by using a small python script.

Example: get player status
pi@P1volumio:~$ ./spconnect_ctrl status active
False
Example: send pause to player
pi@P1volumio:~$ ./spconnect_ctrl pause
OK

The code of the python script “spconnect” can be found in my previous post.

Hello,

I tried to do the script and it works once!

But now I have this:

Loading Spotify library…
SpInit: 0
Traceback (most recent call last):
File “main.py”, line 29, in
connect_app = Connect(web_error_callback)
File “/usr/src/app/connect.py”, line 89, in init
lib.SpPlaybackUpdateVolume(mixer_volume)
OverflowError: can’t convert negative number to unsigned

Is anyone have an idea to solve this?

Thank you

Folks
This worked like a charm with my hifiberry DAC, however when using my Sabre DAC which does not have a volume control I keep getting this error:
Traceback (most recent call last):
File “main.py”, line 9, in
from connect import Connect
File “/usr/src/app/connect.py”, line 8, in
from console_callbacks import audio_arg_parser, mixer, error_callback, connection_callbacks, debug_callbacks, playback_callbacks, playback_setup
File “/usr/src/app/console_callbacks.py”, line 16, in
audio_arg_parser.add_argument(’–mixer’, ‘-m’, help=‘alsa mixer name for volume control’, default=alsa.mixers()[0])
IndexError: list index out of range

I tried some of the solutions listed but no joy. I really love this feature and it really make this whole setup GREAT!
PLEASE, help.

UPDATE:
post on this topic by Twirre of Feb13@ 15:07 works for me. I had finger problems.
TXS everyone.
Txs
Jim

What I’m looking for is being able to switch between airplay and spotify connect. If connect is running I can’t choose airplay without disabling the spotify connect process.

Hi all,

I managed to run the script, but now I would like to automatically start it at boot …

Apparently must write the command in “rc.local” but it’s doesn’t work for me (I am a new user RSBpi). here is my code in rc.local :

[code]#!/bin/sh -e

rc.local

This script is executed at the end of each multiuser runlevel.

Make sure that the script will “exit 0” on success or any other

value on error.

In order to enable or disable this script just change the execution

bits.

By default this script does nothing.

/var/www/command/player_wdog.sh startup & > /dev/null 2>&1

sudo ./pi/spotify-connect-web.sh --username xxxxx --password xxxx --bitrate 320 &

exit 0[/code]

But nothing happens at boot.

thank for help

Hi,
try rathersu pi -c /home/pi/spotify-connect-web.sh --username xxxxx --password xxxx --bitrate 320 &

Thank you for help, but nothing happens :frowning:

with the terminal I tape cd /etc, and " sudo /root/spotify-connect-web.sh --username xxxx --password xxxx --bitrate 320 " it’s work, no problem.

in rc.local I try

sudo /root/spotify-connect-web.sh --username xxxx --password xxxx --bitrate 320 & , doesn’t work…

I don’t know log in “pi” on volumio, only “login :root, password : volumio”. Whatever I do, spotify still intalled in / root. is it important ?

Hi all,
Many thanks for your helpful posts - complete beginner with Rpi/code but I’ve managed to follow instructions to get spotify connect working along with volumio.

I’m now trying to come up with a ‘One Touch’ operation so that the RPi will automatically become the active source through my AV Receiver (Yamaha) whenever I send it a play command via spotify connect.

I’ve got as far as installing Lib-cec into home/pi (same dir as the connect files), and I can get my AVR to switch on and onto the correct source channel for RPi using the command:

echo 'as 5' | cec-client -d 1 -s

(where ‘5’ is the device number for the AVR)

What I haven’t got a clue on is how to get this command to run every time I select Rpi as the active device via spotify connect on my phone app.
I’d appreciate any pointers as to what could be used as a trigger to return this command. I think it may involve writing a bash script but wouldn’t know where to start!?

Thanks for any help on this,
Tom

—EDIT—

I’ve come across a more useful method which checks for any audio output from ALSA:

grep RUNNING /proc/asound/card*/pcm*/sub*/status

I’ve tied this with the active source command in my first ever script:

[code]#!/bin/bash

if grep -q ‘RUNNING’ /proc/asound/card*/pcm*/sub*/status ; then
echo ‘as’ | cec-client -d 1 -s
fi
[/code]
It works!
Now when the command is run and there is audio playing through RPi it will automatically wake my AVR and switch to the correct source.

Next step is to have the script running repeatedly so that it works without me at my laptop to run the script via PuTTY. Any advice on how this is best done? The script is currently sat in ~/home/pi with the volumio and spotify connect files (I’ve named it yamaha-on.sh).
Will it need to include a command to prevent continued execution of the script once the active source command has been activated i.e. when I’m playing music? Or can I just let it continue to run (repeating the ‘as’ command doesn’t seem to have any negative effect on the audio output).

Hi!

First time poster :slight_smile: I’ve read the entire thread multiple times and have been able to install Fornoths github for spotify connect. However, when switching from SC to Airplay, Airplay simply isn’t availiable.

I’ve been meaning to try Tommphs fork instead, but I can’t figure out how to install the fork instead of Fornoths master on my RPi 2. Could someone perhaps shead some light on the issue for me?

Thanks in advance!

// dunderklumpen_swe

Another question :slight_smile:

How do i get zeroconf (avahi) working? Ideally I would like all Spotify users (with premium account) in my household to be able to use the connected speakers without having to logoff/logon via the web-ui. Shouldn’t this be possible using avahi?

Thanks!

/ dundeklumpen_swe

Hey all,

Let me start by saying that I am a total rPI noob and that I bought a preinstalled rPI/hifiberry digi+/volumio install so I am currently enjoying the basic volumio capabilities without having needed any rPI/linux knowledge.
I would however like to be able to use spotify connect and I have been going through this thread but I’d like to know if there is a step by step guide somewhere for… well… rPI/linux noobs.

Thanks!

Hey!
I’ve been struggeling with copying the ‘spotify_appkey.key’ to the directory.
Can anyone please help me with an easy instruciton how to do it 101 :wink:
I’m an novice in this and havn’t been around using phyton for so long so I would appreciate if someone could take som time and help me.

I have downloaded the key and copied to an USB device, whats next?

Maybe it is easier for you to use WinSCP if you are used to Windows. This way you can use your pc to copy the app.key to the correct folder. Just make an ssh connection to the ip address of the decice, browse to the correct folder and drag the file from desktop into the folder.

Dunno if it’s already shared, but this link proved to be really usefull for me:
powerpi.de/spotify-connect-auf-d … bedienung/
Google translate will help, I guess.

Why You not trying Spotify Premium apk? In Android? It’s Great and having much better Features I am using From 3 Months And not faced a Single issue Yet! Hope You Understand What i want to say!

Sorry to resurrect such a zombie post and citing a message from 2015, but Balbuze, how did you do it?

I got it working in 1.55, and I was able to add a line in rc.local for it to start at boot. With volumio2 I am able to install it following Fornoth’s instructions, (it complains a couple of times), but I am not able to run it at startup, because if I do it, it starts as root, and doesn’t find the required folders (looks in root’s home) and if I try to run it as the “volumio” user, it asks for my user password, and then fails (I wasn’t able to pass the password to the command, nor worked trying to add the appropriate commands to the sudoers file in order to be able to run it without passwords).

Obviously, the problem it’s me and I’m sure that there is a straightforward way to do it, but I’m at loss. Is there a way to be able to run the script at startup, as the “volumio” user, without the need of typing the password?

I loaded the latest Hotfix RC2 and installed the Spotify Plugin.

DUDE!!! Its awesome, appears in the Media Browser and can add songs from my Spotify Playlists to my Volumio Playlist.

The only strange thing is on another device running the Spotify app. you can’t control the Volumio Spotify Plugin. It doesn’t show.
But I have control on the Volumio GUI.