Making a music player with raspberry pie

Time:2020-10-12

I bought a raspberry pie 3B +. After installing the system, the ash fell to a thick layer. I didn’t know what to do… Recently, I came up with an idea: make a music player with raspberry pie, play English listening automatically from 6:30 to 7:20 every day, force yourself to practice English, and cure your lazy bed habit, which can also be used to listen to songs.

preparation:

1. One raspberry has been sent, and the Linux system has been installed, and the python 3 environment has been installed;

2. A speaker (aux cable interface or Bluetooth speaker is OK), connected to raspberry pie;

Software environment construction:

MTOOL is a music player written in Python. It is open-source on gitee and GitHub. It is mainly controlled by the command line. This is very suitable for our needs.

Download address: https://gitee.com/shadowlesswalker/mtool.git

First of all, we log in to raspberry pie with SSH tool (putty I used) on the computer. If it won’t, we can also use Baidu raspberry pie SSH tutorial. Enter the following command:

cd /usr

git clone https://gitee.com/shadowlesswalker/mtool.git MTOOL? Download MTOOL source code

cd mtool

Ln – S – F / usr / MTOOL / mtoolctl / usr / bin / MTOOL

Then install the MTOOL dependency packages: pyGame, configparser (if any, you don’t need to install them)

sudo apt install python3-pygame

pip3 install configparser

At this point, the installation of MTOOL is complete. MTOOL is a C / S (server / client) mode program. After the server is turned on, it will listen to the UDP port (the default is 127.0.0.1:6666), and then accept the command from the client to control the playback.

Let me set up the server-side boot

vim ~/.bashrc

Add the command on the last side:

Nohup MTOOL — server start > / usr / MTOOL / log & where nohup and & are used to run programs in the background

Configure Music folder path

First, configure the location of your music folder and open / usr / MTOOL/ mtool.conf , modify it by yourself:

[player] this section sets the player parameters

List = music ා set the default play list. The list name must appear in the playlists section

Volume = 0.2 ා default volume

Port = 6666 ා the default UDP port for the server to accept commands

Index = 2 ා current playing position

Next = next # default playback mode: next | loop | random play corresponding sequence | single loop | random play

[playlists] # preset sections of the play list, which can be set in the format: list name = folder path

Music = music ා the name of the play list is music, and the path is. / music

EN listen = / var / share / en listen ා the path of my English listening resource file

Start using the MTOOL command

source  ~/.bashrc

or

nohup mtool –server start > /usr/mtool/log &

#Start the server manually first. Although the previous configuration is boot up, it can only take effect after the next boot

MTOOL – C info ා view the server status, which can be used to test whether the server has been started. The attack is similar to the following display:

 


MTOOL – C play ා

MTOOL – C Vol = 0.5 ᦇ set volume

MTOOL – C lists ා view the available playlists

MTOOL – C list ා lists the music files in the current play list

MTOOL – C next = random | next | loop ා set playback order

MTOOL – C playf = Zui ා switches to single loop (next = loop), and plays the file with Zui in the file name

The basic usage is like this, we can control raspberry pie to play music on the computer, but the computer is always on is not good, what’s more, lying in bed? We can install SSH tools on mobile phones, such as juicessh (recommended) and alicloud app.

Set raspberry pie to play regularly

We use crontab, a Linux built-in task scheduling command, to set the scheduled playback function.

When it comes to timing tasks, we first need to make sure that the time of our raspberry pie is accurate. Hey, hey, hey… Unfortunately, raspberry pies are usually inaccurate… This is because the clock will continue to run when the computer is powered off by a CMOS battery, but the time will be lost after the raspberry pie is turned off. Even if the time is not accurate, how can you normally perform a scheduled task…

So let’s solve the time problem first – NTP (network time synchronization)

dpkg-reconfigure tzdata    #Set the time zone. The configuration file is in / etc / timezone and set to Shanghai time zone

dateා check the time and make sure our time zone is China time zone

Start NTP service

apt install ntp

ntpdate ntp.ntsc.ac.cn       #Synchronization of new NTP server of China National Time Service Center ntp.ntsc.ac .cn)

Or in / etc/ ntp.conf Add NTP server address in

Restart NTP service:

Systemctl enable NTP ා NTP

Systemctl start NTP ා start NTP service

Date ා check the system time, it should be correct

Then set the scheduled task:

First, write a script to switch the play list, change the volume, and play when on, which is called by the scheduled task

vim  /usr/mtool/start-en-listen

The script is simple:

_dir=”/usr/mtool”

MTOOL – C playlist = en listen ා switch to English listening list

MTOOL – C Vol = 1.0 ᦇ turn the volume to maximum (hehe hehe)

MTOOL – C next = random ා set random play

MTOOL – C play ා

Next, add a scheduled task

Crontab – e ා will open the editor and add the scheduled task at the end:

30 6 * * * /usr/mtool/start-en-listen >> /usr/mtool/ log.client It starts at 6:30 every morning

20 7 * * MTOOL – C stop

 

Finally… Great achievements!!!! I can’t sleep in every morning!!!

Of course, we can also add a few lines of code, automatically turn off the SSH service at 6:30 a.m., and then turn it on at 7:20 a.m., so that when you want to sleep in, you can’t turn off raspberry pie playback from the SSH end of your mobile phone, and you can only get up and unplug the power…..

Recommended Today

The course of using Chinese software of poedit Pro

Poedit pro (formerly known as poedit) is a free (professional version charge), open source and cross platform gettext class (. Po format, gettext is used in the application program for program internationalization) International Translation editor. It is also one of the most widely used software of the same type. At present, it is available in […]