Spotify Connect

Discuss, contribute and orient Volumio development!

Ads helps Volumio remain Free and Open Source. Please consider donating to help us continue to serve you.

Spotify Connect

Postby MrSponti » Mon Feb 29 2016 17:45

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]

Code: Select all
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: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-

#
# Get status from Spotify Connect Client on Volumio
#
# Creation:    29.02.2016
# Last Update: 29.02.2016
#
# Copyright (c) 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()


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

Code: Select all
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.
Random avatar
MrSponti
Fresh off the boat
Fresh off the boat
 
Posts: 4
Joined: Sun Feb 14 2016 09:07

Ads helps Volumio remain Free and Open Source. Please consider donating to help us continue to serve you.


Spotify Connect

Postby toek » Wed Mar 09 2016 06:49

MrSponti wrote:Hi,

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


Any updates on this? Pretty important feature to get the family on board.
Random avatar
toek
Fresh off the boat
Fresh off the boat
 
Posts: 4
Joined: Wed Feb 10 2016 10:57


Spotify Connect

Postby arjenr » Wed Mar 09 2016 08:52

What do you mean by killing the whole process? You can stop playing Spotify Connect by selecting another device in the Spotify app.
Random avatar
arjenr
Fresh off the boat
Fresh off the boat
 
Posts: 9
Joined: Tue Jan 19 2016 09:15


Spotify Connect

Postby MrSponti » Wed Mar 09 2016 18:38

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.
Random avatar
MrSponti
Fresh off the boat
Fresh off the boat
 
Posts: 4
Joined: Sun Feb 14 2016 09:07


Spotify Connect

Postby Thiboo3 » Sun Mar 13 2016 10:46

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 <module>
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
Random avatar
Thiboo3
Fresh off the boat
Fresh off the boat
 
Posts: 1
Joined: Sun Mar 13 2016 10:43


Spotify Connect

Postby jocoman » Tue Mar 15 2016 17:29

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 <module>
from connect import Connect
File "/usr/src/app/connect.py", line 8, in <module>
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 <module>
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
Random avatar
jocoman
Sunday DIYer
Sunday DIYer
 
Posts: 28
Joined: Tue Sep 29 2015 20:49


Spotify Connect

Postby toek » Fri Mar 18 2016 06:53

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


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.
Random avatar
toek
Fresh off the boat
Fresh off the boat
 
Posts: 4
Joined: Wed Feb 10 2016 10:57


Spotify Connect

Postby kizery34 » Thu Mar 24 2016 18:13

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: Select all
#!/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


But nothing happens at boot.

thank for help
Random avatar
kizery34
Fresh off the boat
Fresh off the boat
 
Posts: 2
Joined: Thu Mar 24 2016 18:07


Spotify Connect

Postby balbuze » Thu Mar 24 2016 20:31

Hi,
try rather
Code: Select all
su pi -c /home/pi/spotify-connect-web.sh --username xxxxx --password xxxx --bitrate 320 &
User avatar
balbuze
Moderator
 
Posts: 1619
Joined: Thu May 08 2014 08:42
Location: Isère France


Spotify Connect

Postby kizery34 » Tue Mar 29 2016 20:05

Thank you for help, but nothing happens :(

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 ?
Random avatar
kizery34
Fresh off the boat
Fresh off the boat
 
Posts: 2
Joined: Thu Mar 24 2016 18:07

PreviousNext

Return to Development talks

Who is online

Users browsing this forum: No registered users and 0 guests