KanoMaster22
Posts: 55
Joined: Wed Oct 05, 2016 6:06 pm

Looping an mp3

Wed Oct 18, 2017 6:06 pm

Is there as easy way to loop an mp3? I have tried:

Code: Select all

import os
import time

while True:
        os.system('omxplayer /home/pi/fluffing_a_duck.mp3') # Plays fluffing a duck
        time.sleep(68) # wait for the length of the music then starts the loop again
I thought this would play the song and then after 68 seconds (the length of the song) it would play it again for infinity time (or until I kill the program). However, this is not the case and it plays the song once then stops but it is worth noting the script doesn't end, it just does nothing until I kill the program. Can anyone adapt my program to make it loop my song?

Thanks in advance for any help,
LouisP
Last edited by KanoMaster22 on Thu Oct 19, 2017 5:54 am, edited 3 times in total.

User avatar
rpdom
Posts: 11719
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK

Re: Looping an mp3

Wed Oct 18, 2017 7:59 pm

Are you sure it doesn't play the track, then wait for 68 seconds before playing it again?

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

Re: Looping an mp3

Wed Oct 18, 2017 8:37 pm

Is this your actual code? There's a spelling mistake in os.system
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

KanoMaster22
Posts: 55
Joined: Wed Oct 05, 2016 6:06 pm

Re: Looping an mp3

Thu Oct 19, 2017 5:53 am

No this isn't the exact code as I wrote this on my PC after I had turned of my pi. Now copying the script off my pi.

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

Re: Looping an mp3

Thu Oct 19, 2017 6:55 am

I think this is a good example of when using subprocess may be better. I can give you some code once I get into work!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

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

Re: Looping an mp3

Thu Oct 19, 2017 7:00 am

Or have a go yourself! subprocess.call will run the command and wait until it finishes before running the next line.

The python docs generally recommend using subprocess instead of os.system anyway so it would be good to take a look.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

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

Re: Looping an mp3

Thu Oct 19, 2017 8:18 am

rpdom wrote:
Wed Oct 18, 2017 7:59 pm
Are you sure it doesn't play the track, then wait for 68 seconds before playing it again?
Having looked at the docs, it sounds like this would be the expected behaviour as the return value is the exit status which would only be available once the command has completed.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
OutoftheBOTS
Posts: 407
Joined: Tue Aug 01, 2017 10:06 am

Re: Looping an mp3

Thu Oct 19, 2017 10:58 am

First of all you can play MP3s from within python using the PyGame library without needing to run an external process like "os.system('omxplayer /home/pi/fluffing_a_duck.mp3')"

As you open more processes then it has a number of effects, first the OS now has to run 2 programs at once and this adds extra over heads for the processor and can bog down the processing speed of all your processes, second the more processes you run at once the higher the chance that 2 processes can conflict with each other(this is very hard to debug)

using either os.system or subprocess has this effect. Good programmers will only use multi processes when the task requires it. Here your just wanting to do 1 thing at a time, play a MP3 then when it finishes play it again.

Here's code that I tapped out in note++ untested but it should work without the need for multiprocessing :)

Code: Select all

import pygame
pygame.mixer.init()
pygame.mixer.music.load("/home/pi/fluffing_a_duck.mp3")

while True:
  pygame.mixer.music.play()
  while pygame.mixer.music.get_busy() == True: pass
 

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

Re: Looping an mp3

Thu Oct 19, 2017 12:06 pm

OutoftheBOTS wrote:
Thu Oct 19, 2017 10:58 am
using either os.system or subprocess has this effect. Good programmers will only use multi processes when the task requires it. Here your just wanting to do 1 thing at a time, play a MP3 then when it finishes play it again.
Ha - I guess I'm a "bad" programmer then!

I'd agree that pygame is worth looking at but I've had mixed success with mp3 files (the docs say support for the format is limited) but no harm in trying.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

KanoMaster22
Posts: 55
Joined: Wed Oct 05, 2016 6:06 pm

Re: Looping an mp3

Thu Oct 19, 2017 5:31 pm

Thanks for all the help guys! I tried the pygame method and it works perfectly!

User avatar
OutoftheBOTS
Posts: 407
Joined: Tue Aug 01, 2017 10:06 am

Re: Looping an mp3

Thu Oct 19, 2017 9:20 pm

OutoftheBOTS wrote: ↑
Thu Oct 19, 2017 8:58 pm
using either os.system or subprocess has this effect. Good programmers will only use multi processes when the task requires it. Here your just wanting to do 1 thing at a time, play a MP3 then when it finishes play it again.

Ha - I guess I'm a "bad" programmer then!
I think it is better to understand what mutli processing is and the advantages and disadvantages of it.

Have you ever had lots of things open on your computer all at once and your computer starts to run really slowly and then if too many things r open the computer can freeze or one of the things open will crash, or if you run 2 certain programs at the same time they always crash. This is classic mulity processing problems

Multi processing requires over heads from the CPU to maintain (usually done by the OS), it trys to check nothing is conflicting and a well optomised OS will shift different processes onto different cores which means it is possible to have 2 processes running in parallel. This means it is possible (if the OS isn't perfect in checking) for 2 different processes to access the same feature at the same time causing conflict and corruption of data i.e both try to write to screen at exactly same time.

Now the classic example that is given where mutli processing is in its prime is a web crawler. If you want to make a web crawler to search many sites it would be very slow to do it 1 site at a time because you will be idle most of the time waiting for the website to reply, so you use multi processing where each website is on it's own process running in parallel so while it is waiting for 1 website to respond a different process is accessing another website.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 13 guests