gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

MP3 player

Mon Sep 02, 2019 1:00 pm

To replace my ageing PC which is used to play my mp3 music l wrote a python script for the Pi.

If you want to try it it's on github

https://github.com/Gordon999/Pi-MP3_Player
Attachments
lcd.jpg
lcd.jpg (75.86 KiB) Viewed 460 times
Last edited by gordon77 on Sun Oct 20, 2019 8:56 am, edited 4 times in total.

scotty101
Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: MP3 player

Mon Sep 02, 2019 1:16 pm

Would you be open to some constructive feedback on your code?
There are a few things that could help improve your code and make it more "pythonic"
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Mon Sep 02, 2019 1:27 pm

scotty101 wrote:
Mon Sep 02, 2019 1:16 pm
Would you be open to some constructive feedback on your code?
There are a few things that could help improve your code and make it more "pythonic"
No problem.

Isn't that what github is about ?

scotty101
Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: MP3 player

Mon Sep 02, 2019 3:40 pm

All intended to be constructive and in the spirit that code is read more often than it is written

Code: Select all

E8 = tk.Text(self.master, height=1, width=3, font = 40, padx = 10, pady = 10)
E8.place(x=595, y=16)
E8.insert(INSERT,volume)
I can infer from this that E8 is for the volume control but it isn't obvious from the name. Try something like entryVolume instead. Good variable naming might feel like having to type an extra few characters but it helps when you come to debug a problem later.
Also, with tkinter, whilst place is an allowable way to layout widgets, it doesn't work well when it comes to different screen sizes (as you mentioned on the README). Use grid or pack instead.

Code: Select all

track = "/media/pi/" + drive_name + "/" + artist_name + "/" + album_name + "/"  + track_name
Code paths are always a problem when it comes to portability but python makes some of this easier. For example you could change the above to

Code: Select all

track = os.path.join('/media/pi/',drive_name, artist_name, album_name, track_name)
os.path.join will add the correct separator.
Or if you want to use the latest, have a look at the pathlib module

Code: Select all

if len (Tracks) > 0 :
            for counter in range (0,len(Tracks)):
                drive_name  = (Tracks[counter].split('/')[3])
Could very easily be replaced with something more python friendly and in fewer lines of code. Using an index to iterate through items is very common in languages such as C but with more modern languages, you can just iterate though each item like show below. This will also deal with the case that there are no tracks available.

Code: Select all

for track in Tracks:
    drive_name = track.split('/')[3]
Globals..... You've made use of many of them despite also using Object Oriented programming techniques. Make the variables properties of the class rather than globals. For example rather than having to write "global playing" at the top of each function, make playing a class property with "self.playing = 0" and then use it with "if self.playing:"

Code: Select all

E2.delete('1.0','50.0')
I'm guessing you are trying to delete the contents of the box. You can do the same thing with

Code: Select all

E2.delete(1.0, END)

This will delete everything not just up to the 50th row, 0th character.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Mon Sep 02, 2019 4:06 pm

Thanks scotty101.

All taken in good faith, always useful to learn.

I'll get these suggestions included.

Gordon

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Wed Sep 04, 2019 3:21 pm

I have implemented most of scotty101's suggestions, except using .grid which still confuses me when I try to get the required layout !, and fixed a few issues.

cmrincon
Posts: 40
Joined: Thu May 24, 2018 7:39 pm

Re: MP3 player

Fri Sep 06, 2019 7:22 am

Have you thought about add m3u song lists?

It would be a very nice feature.

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Fri Sep 06, 2019 7:30 am

cmrincon wrote:
Fri Sep 06, 2019 7:22 am
Have you thought about add m3u song lists?

It would be a very nice feature.
What features would you like from m3u?

cmrincon
Posts: 40
Joined: Thu May 24, 2018 7:39 pm

Re: MP3 player

Fri Sep 06, 2019 7:45 am

(repeated post)
Last edited by cmrincon on Fri Sep 06, 2019 8:36 am, edited 1 time in total.

cmrincon
Posts: 40
Joined: Thu May 24, 2018 7:39 pm

Re: MP3 player

Fri Sep 06, 2019 7:45 am

Hi!

it would be nice: reading a m3u list and play its songs, also adding a feature to create m3u lists in order to save your own lists can be a nice feature.

I saw that you devised your own format list. I don't understand why you did this, and why you didn't use m3u or any other. I understand that this makes the project more complex, but maybe if you are patient enought i can help with a m3u python parser.

Cesar.

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Fri Sep 06, 2019 8:36 am

Removed
Last edited by gordon77 on Fri Sep 06, 2019 5:20 pm, edited 2 times in total.

cmrincon
Posts: 40
Joined: Thu May 24, 2018 7:39 pm

Re: MP3 player

Fri Sep 06, 2019 11:32 am

i found a python m3u parse. It can be useful:

Take a look at:
https://github.com/Timmy93/M3uParser


gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Wed Oct 02, 2019 3:33 pm

Hopefully most of the bugs are out of this now...

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Fri Nov 29, 2019 11:21 am

Updated... Even more bugs removed !!

https://github.com/Gordon999/Pi-MP3_Player

RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: MP3 player

Mon Dec 02, 2019 10:57 pm

Hi
I am very interested in an mp3 player but after loading the program, I get the following error in Terminal when trying to follow the installation instructions:

sudo apt-get install python3-alsaaudio

Code: Select all

Unable to locate package python3-alsaaudio
I followed the remaining items and all the other packages appear to have loaded correctly, in that no further errors were reported.

Running the program then reports (as expected)

Code: Select all

ImportError: No module named 'alsaaudio'
Is there any other way of installing alsaaudio?

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Tue Dec 03, 2019 9:24 am

Try it again, I have just built a new SD card and it all installed OK

RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: MP3 player

Tue Dec 03, 2019 2:52 pm

Hi
Just tried again, same result.

Code: Select all

Unable to locate package python3-alsaaudio

I think my previous attempt when it just reported:

Code: Select all

Unable to locate package alsaaudio
was because I had tried it without the 'python3-' part.

Could it be that my SD card image needs updating, because I have been using the same image for quite a while now.

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Tue Dec 03, 2019 3:37 pm

Are you using Buster ?

or try..

Code: Select all

sudo -H pip3 install pyalsaaudio

RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: MP3 player

Tue Dec 03, 2019 4:44 pm

@gordon77
No, on this Pi, I am using Stretch, V9.
I have not upgraded all my Pi's to Buster.

Anyway, your alternative solution has worked and I now have a Tkinter window display (some of the boxes in the Tkinter window are overlapping each other but still usable and not a problem)

I will now load some mp3 files to test it out further.
Thank you very much for your help.

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Tue Dec 03, 2019 4:58 pm

Upload a screenshot and let me know the screen resolution

RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: MP3 player

Tue Dec 03, 2019 7:08 pm

2019-12-03-174811_1280x720_scrot.png
2019-12-03-174811_1280x720_scrot.png (79.92 KiB) Viewed 114 times
I am not sure about the screen resolution unless of course that is it, contained in the filename.

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Tue Dec 03, 2019 7:11 pm

1280x720 l'll assume.

Is it showing fullscreen?

A bit of work for me to do!

RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: MP3 player

Tue Dec 03, 2019 7:18 pm

Please, no extra work on my behalf.
What is the resolution recommended?

gordon77
Posts: 4292
Joined: Sun Aug 05, 2012 3:12 pm

Re: MP3 player

Tue Dec 03, 2019 7:24 pm

It was written for the Pi 7" Display (800x480), but works fine on my monitor at 1920x1080 but not at fullscreen.

Return to “Python”