Basic Spotify connect Speaker

I like to close the Spotify application on my phone and still listen to some background music. This is only possible with another device. I run GitHub - Spotifyd/spotifyd: A spotify daemon Let’s make a spotifyd.service.

Use superuser

instead of using the normal user we use superuser and put the contents in to /etc/systemd/system. it is workin good so far.

I don’t know why but sometimes I run a sudo cronjob to restart the job. I’ve removed the job on 2024-05-02.

[Unit]
Description=A spotify playing daemon
Documentation=https://github.com/Spotifyd/spotifyd
Wants=sound.target
After=sound.target
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/home/user/Downloads/spotifyd --config-path /home/user/Documents/spotify.txt
Restart=always
RestartSec=12
RemainAfterExit=yes

[Install]
WantedBy=default.target

[global]
# Your Spotify account name.
username = "xxx"
 
# Your Spotify account password.
password = "xxx"
 
# A command that gets executed and can be used to
# retrieve your password.
# The command should return the password on stdout.
#
# This is an alternative to the `password` field. Both
# can't be used simultaneously.
#password_cmd = "command_that_writes_password_to_stdout"
 
# If set to true, `spotifyd` tries to look up your
# password in the system's password storage.
#
# Note, that the `password` field will take precedence, if set.
#use_keyring = false
 
# If set to true, `spotifyd` tries to bind to dbus (default is the session bus)
# and expose MPRIS controls. When running headless, without the session bus,
# you should set this to false, to avoid errors. If you still want to use MPRIS,
# have a look at the `dbus_type` option.
use_mpris = false
 
# The bus to bind to with the MPRIS interface.
# Possible values: "session", "system"
# The system bus can be used if no graphical session is available
# (e.g. on headless systems) but you still want to be able to use MPRIS.
# NOTE: You might need to add appropriate policies to allow spotifyd to
# own the name.
dbus_type = "system"
 
# The audio backend used to play music. To get
# a list of possible backends, run `spotifyd --help`.
backend = "alsa" # use portaudio for macOS [homebrew]
 
# The alsa audio device to stream audio. To get a
# list of valid devices, run `aplay -L`,
#device = "alsa_audio_device"  # omit for macOS
 
# The PCM sample format to use. Possible values 
# are F32, S32, S24, S24_3, S16. 
# Change this value if you encounter errors like
# "Alsa error PCM open ALSA function 'snd_pcm_hw_params_set_format' failed with error 'EINVAL: Invalid argument'"
#audio_format = "S16"
 
# The alsa control device. By default this is the same
# name as the `device` field.
#control = "alsa_audio_device"  # omit for macOS
 
# The alsa mixer used by `spotifyd`.
#mixer = "PCM"  # omit for macOS
 
# The volume controller. Each one behaves different to
# volume increases. For possible values, run
# `spotifyd --help`.
#volume_controller = "alsa"  # use softvol for macOS
 
# A command that gets executed in your shell after each song changes.
#on_song_change_hook = "command_to_run_on_playback_events"
 
# The name that gets displayed under the connect tab on
# official clients. Spaces are not allowed!
device_name = "Luca-Speaker"
 
# The audio bitrate. 96, 160 or 320 kbit/s
#bitrate = 320
 
# The directory used to cache audio data. This setting can save
# a lot of bandwidth when activated, as it will avoid re-downloading
# audio files when replaying them.
#
# Note: The file path does not get expanded. Environment variables and
# shell placeholders like $HOME or ~ don't work!
#cache_path = "cache_directory"
 
# The maximal size of the cache directory in bytes
# The example value corresponds to ~ 1GB
#max_cache_size = 1000000000
 
# If set to true, audio data does NOT get cached.
#no_audio_cache = true
 
# Volume on startup between 0 and 100
# NOTE: This variable's type will change in v0.4, to a number (instead of string)
initial_volume = "10"
 
# If set to true, enables volume normalisation between songs.
#volume_normalisation = true
 
# The normalisation pregain that is applied for each song.
#normalisation_pregain = -10
 
# After the music playback has ended, start playing similar songs based on the previous tracks.
autoplay = true
 
# The port `spotifyd` uses to announce its service over the network.
#zeroconf_port = 1234
 
# The proxy `spotifyd` will use to connect to spotify.
#proxy = "http://proxy.example.org:8080"
 
# The displayed device type in Spotify clients.
# Can be unknown, computer, tablet, smartphone, speaker, t_v,
# a_v_r (Audio/Video Receiver), s_t_b (Set-Top Box), and audio_dongle.
device_type = "audio_dongle"
 

Spotify automated alarm Clock

Now (2024-08-19) I’ve noticed I want to have my alarm on my speakers.

there is currently a bug in spotifyd (actually upstream librespot) so I was using librespot-auth. Generate credentials.json and access it with librespot (using the directory with the credentials).

Best is Raspotify (or you have to use cargo to compile librespot yourself). The best of it is that you can do credentials caching here.

To control it I use cargo built spotify_player from my server. Authentication is same as librespot (use the dircetory wtih the cerdentials.json with -C /path). id of orangepi: e3a990a7cfec8e0c640b130e873bf705bbed6333 lets say I have a file: musicalarm.sh

~/.cargo/bin/spotify_player -C . connect --id e3a990a7cfec8e0c640b130e873bf705bbed6333
 
~/.cargo/bin/spotify_player -C . playback volume 60
 
~/.cargo/bin/spotify_player -C . playback start liked -r -l 10

There are interesting features of the cli. I use it to set the volume to 60% and play 10 of my liked songs in random order and then stop. Naturally it has spotify connect and I control it via the spotify app on other devices (mostly my phone).

We can add a cronjob: 0 8 * * 1-5 bash /home/musicalarm.sh to start the music every time to get up during the week. To actually have the correct time we have to configure the timezone: sudo timedatectl set-timezone Europe/Berlin. Because we configured the small SBC on my stereo with a systemd service and the server to control the music with a cronjob it should automatically work after reboots and no further configuration is needed.