julianngrant
Posts: 5
Joined: Mon Mar 03, 2014 11:00 am

playing random mp3 in python

Mon Mar 03, 2014 11:17 am

Hi I hope somebody can help me with what is hopefully a simple problem.

I am trying to play a random mp3 from a folder of sound files for a robot project i am working on.

Through trawling these forums i have put together some code but when i run it the beginning of each m3 it plays is missing. In other words it starts a couple of seconds in to each file instead of from the start.

The code is pasted below.

#!/usr/bin/env python
import os, random

def rndmp3 ():
randomfile = random.choice(os.listdir("/home/pi/mp3"))
file = ' /home/pi/mp3/'+ randomfile
os.system ('omxplayer' + file)

rndmp3 ()

It does not matter if i replace omxplayer with mpg123 or mpg321 I get exactly the same result.

If I remove the random element and just insert one of the file names directly into the code it plays it from the start ok so I am guessing that it is something to do with the time taken to calculate the string 'file'. I've tried adding a delay between the line where it calculates 'file' and the os.system ('omxplayer' + file) line where it plays it but that makes no difference.

Thanks in advance for any help I can get with this.

kyle2k
Posts: 84
Joined: Tue Feb 19, 2013 2:47 pm

Re: playing random mp3 in python

Mon Mar 03, 2014 2:43 pm

Hi,

I have tested your code on my model A Pi that has a folder with 110 mp3 files and all is working fine, the songs are played from the start.

Code: Select all

#!/usr/bin/env python
import os, random

def rndmp3 ():
	randomfile = random.choice(os.listdir("/home/pi/music/"))
	file = ' /home/pi/music/'+ randomfile
	os.system ('mplayer' + file)

rndmp3 ()
How many files do you have in your mp3 folder?

kyle2k
Posts: 84
Joined: Tue Feb 19, 2013 2:47 pm

Re: playing random mp3 in python

Mon Mar 03, 2014 4:44 pm

Also have you updated your system?

sudo apt-get update

And

sudo apt-get upgrade

julianngrant
Posts: 5
Joined: Mon Mar 03, 2014 11:00 am

Re: playing random mp3 in python

Mon Mar 03, 2014 5:34 pm

Hi,
Thanks for the reply. I have a model B Pi and there are 233 songs in the folder. I have also got the latest updates.
I'm intrigued that it worked fine for you though so that sounds promising.
I'm out right now but will try later with less files in the folder and see what happens.
Thanks again.

kyle2k
Posts: 84
Joined: Tue Feb 19, 2013 2:47 pm

Re: playing random mp3 in python

Mon Mar 03, 2014 5:49 pm

Hi again,

I have just noticed that when I altered your code with the path to my music files that I also changed the player from omxplayer to mplayer.

I am also away from my Pi at the moment but will try it again later with omxplayer and report back.

julianngrant
Posts: 5
Joined: Mon Mar 03, 2014 11:00 am

Re: playing random mp3 in python

Mon Mar 03, 2014 7:28 pm

OK thx I will also try mplayer when I get in later

kyle2k
Posts: 84
Joined: Tue Feb 19, 2013 2:47 pm

Re: playing random mp3 in python

Mon Mar 03, 2014 8:39 pm

I have just tested again using the code below playing via Omxplayer and all songs play from the start of the file.

I am using the 3.5mm headphone jack and a xmini speaker and not hdmi sound output.

Code: Select all

#!/usr/bin/env python
import os, random

def rndmp3 ():
   randomfile = random.choice(os.listdir("/home/pi/music/"))
   file = '/home/pi/music/'+ randomfile
   os.system ('omxplayer' + file)

rndmp3 ()

julianngrant
Posts: 5
Joined: Mon Mar 03, 2014 11:00 am

Re: playing random mp3 in python

Tue Mar 04, 2014 10:05 am

Hey thanks for your help.

You gave me a great clue. Not sure why it works but here's the solution.

#!/usr/bin/env python
import os, random

def rndmp3 ():
randomfile = random.choice(os.listdir("/home/pi/mp3"))
file = ' /home/pi/mp3/'+ randomfile
os.system ('omxplayer -o local' + file)

rndmp3 ()

I was testing the program through the speakers in my HDMI display and there was a delay.

But when i add '-o local' to the omxplayer command, which directs the output to the jack socket which is what the robot will use, there is no delay and the mp3's start from the start.

Don't know why it gets chopped through the HDMI but it doesn't matter because I won't be using that output.

Thanks again for taking the time to help me.

kyle2k
Posts: 84
Joined: Tue Feb 19, 2013 2:47 pm

Re: playing random mp3 in python

Tue Mar 04, 2014 11:44 am

Glad to hear that it's working :D

Just out of interest what happens when you force it through hdmi with the '- o hdmi' flag?

Regards Kyle

julianngrant
Posts: 5
Joined: Mon Mar 03, 2014 11:00 am

Re: playing random mp3 in python

Tue Mar 04, 2014 11:55 am

Hi Kyle

When I force it through the hdmi with -o hdmi it chops the front of the mp3 off as before.

Strange that the hdmi behaves this way. Something weird in the driver maybe?

Best regards

Julian

kyle2k
Posts: 84
Joined: Tue Feb 19, 2013 2:47 pm

Re: playing random mp3 in python

Tue Mar 04, 2014 5:53 pm

As you say strange why you get the delay, and also that you had the same with mpg123 player as well?
But happy that you have a workable solution.

All the best with the rest of your robot project.

User avatar
jojopi
Posts: 3561
Joined: Tue Oct 11, 2011 8:38 pm

Re: playing random mp3 in python

Tue Mar 04, 2014 9:46 pm

I suspect it is the display that automatically mutes the audio stream for a second whenever it stops or restarts, or changes sample rate or format.

There is an experimental config.txt option "force_hdmi_open=1" that may be worth trying. As I understand it, it is supposed to force a valid audio stream out even when nothing is playing.

Telemachus
Posts: 2
Joined: Mon May 05, 2014 10:53 pm

Re: playing random mp3 in python

Mon May 05, 2014 10:56 pm

I have used almost the same exact code to play random files using mplayer. It works great if the mp3 files that I play are only one word, ie: bell.mp3. If there are other words with spaces, such as bell chime.mp3 it tries to play chime, which doesn't work, and then bell.mp3, which also doesn't work.

Any suggestions?

gkreidl
Posts: 6345
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: playing random mp3 in python

Tue May 06, 2014 7:24 am

Telemachus wrote:I have used almost the same exact code to play random files using mplayer. It works great if the mp3 files that I play are only one word, ie: bell.mp3. If there are other words with spaces, such as bell chime.mp3 it tries to play chime, which doesn't work, and then bell.mp3, which also doesn't work.

Any suggestions?
file names containing spaces must be quoted:
file = ' \'/home/pi/mp3/'+ randomfile + '\''
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

Telemachus
Posts: 2
Joined: Mon May 05, 2014 10:53 pm

Re: playing random mp3 in python

Tue May 06, 2014 6:00 pm

That did the trick, thanks!

hefeilong1991
Posts: 15
Joined: Wed May 14, 2014 1:38 am

Re: playing random mp3 in python

Thu Jun 05, 2014 9:18 am

how come i use the exact code from you but my model b raspberry have no music play.i have only two music in the folder

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: playing random mp3 in python

Thu Jun 05, 2014 11:41 am

When you say "exact code" there are a number of suggestions above, so please post your code (inside code tags please). I assume your audio files are in "/home/pi/music/".

Lastly, is there an error message? or is there just no audio?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Unimatrix01
Posts: 2
Joined: Wed Feb 04, 2015 7:47 pm

Re: playing random mp3 in python

Fri May 29, 2015 6:06 pm

Hi all, I know that this is a rather old post but I could really do with a little help please!
I have copied this entire code into a text editor and run it as a module in Python, my problem is that when it executes, all I get are two prompt lines and then nothing?
If I go into "Terminal" and type omxplayer opendoor.mp3 It will happily play the file without any issue?

Any help or advice would be very much appreciated


Craig

adamrobots
Posts: 1
Joined: Tue Jan 07, 2014 10:44 am

Re: playing random mp3 in python

Thu Nov 12, 2015 8:19 am

Hi
I'm looking at a similar project. Where exactly should I be pasting the code.
Sorry for the noob question!
Many thanks
Adam

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

Re: playing random mp3 in python

Thu Nov 12, 2015 6:59 pm

Does the title or any of the mp3 details show up on screen when using this code.
If not could someone explain how to display info from the mp3 file.
Thanks

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

Re: playing random mp3 in python

Thu Nov 12, 2015 7:01 pm

@adamrobots
I am new as well but as far as I know, you just load up Python, select a new file which will give you an empty window and then paste the code in there.

JLB7
Posts: 2
Joined: Fri Mar 10, 2017 5:02 pm

Re: playing random mp3 in python

Fri Mar 10, 2017 5:22 pm

Hi, hope someone sees this, I know this is an old post but I am in the same boat as Craig here and would appreciate some help, hope there is a simple solution to the problem.
I have copied this entire code into a text editor and run it as a module in Python, my problem is that when it executes, all I get are two prompt lines and then nothing?
If I go into "Terminal" and type omxplayer opendoor.mp3 It will happily play the file without any issue?
Any help or advice would be very much appreciated
Craig
I would add onto his problem that I have opened idle through the terminal and when i run the program this pops up in the terminal:
sh: 1: omxplayer/home/pi/Track_01.py not found
Any ideas on what this is? I have tried navigating through directories to find the omxplayer folder but it is nowhere.
The code I am using in the program is here:

Code: Select all

#!/usr/bin/env python
import os, random

def rndmp3 ():
   randomfile = random.choice(os.listdir("/home/pi/music/"))
   file = '/home/pi/music/'+ randomfile
   os.system ('omxplayer' + file)

rndmp3 ()

gkreidl
Posts: 6345
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: playing random mp3 in python

Fri Mar 10, 2017 6:23 pm

your os.system call is missing a space:
os.system ('omxplayer ' + file)
Last edited by gkreidl on Sun Mar 12, 2017 6:21 am, edited 1 time in total.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

halek
Posts: 12
Joined: Fri Mar 11, 2016 4:18 pm

Re: playing random mp3 in python

Sat Mar 11, 2017 8:51 pm

JLB7 wrote:Hi, hope someone sees this, I know this is an old post but I am in the same boat as Craig here and would appreciate some help, hope there is a simple solution to the problem.
I have copied this entire code into a text editor and run it as a module in Python, my problem is that when it executes, all I get are two prompt lines and then nothing?
If I go into "Terminal" and type omxplayer opendoor.mp3 It will happily play the file without any issue?
Any help or advice would be very much appreciated
Craig
I would add onto his problem that I have opened idle through the terminal and when i run the program this pops up in the terminal:
sh: 1: omxplayer/home/pi/Track_01.py not found
Any ideas on what this is? I have tried navigating through directories to find the omxplayer folder but it is nowhere.
The code I am using in the program is here:

Code: Select all

#!/usr/bin/env python
import os, random

def rndmp3 ():
   randomfile = random.choice(os.listdir("/home/pi/music/"))
   file = '/home/pi/music/'+ randomfile
   os.system ('omxplayer' + file)

rndmp3 ()
Try if this comes thru, http://www.linuxquestions.org/questions ... 175601093/
In item #10 was presented the way to have omxplayer get output to the HDMI.

Good luck

JLB7
Posts: 2
Joined: Fri Mar 10, 2017 5:02 pm

Re: playing random mp3 in python

Fri Mar 17, 2017 3:17 pm

thanks gkreidl, that fixed my problem, and of course it was so simple, cant believe i didnt see that

Return to “Python”