Trooper11040
Posts: 3
Joined: Sun Oct 13, 2013 5:35 pm

Making MP3 play 1 time on Python Script Bootup

Thu Oct 17, 2013 12:15 am

I have a python script already written, that uses 3 reed switches, and when my magnet runs over them, it makes a MP3 play. But is there any coding I could use, that when my script boots up (I have it running on the Pi's startup) that it can play an MP3 right at the start of the script one time? Thanks...hope that makes sense!!! Below is my code that I am using already successfully so far!
Dan

Code: Select all

#!/usr/bin/env python 

from time import sleep 
import os 
import RPi.GPIO as GPIO 

GPIO.setmode(GPIO.BCM) 
GPIO.setup(23, GPIO.IN) 
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)

while True: 
    if ( GPIO.input(23) == False ): 
      os.system('mpg321 habs.mp3 -q &') 
      sleep(10.0); 
    if ( GPIO.input(24) == False ): 
      os.system('mpg321 habs.mp3 -q &') 
      sleep(10.0);
    if ( GPIO.input(25) == False ): 
      os.system('mpg321 habs.mp3 -q &') 
      sleep(10.0);
    sleep(0.1);

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Making MP3 play 1 time on Python Script Bootup

Thu Oct 17, 2013 6:45 am

How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

Trooper11040
Posts: 3
Joined: Sun Oct 13, 2013 5:35 pm

Re: Making MP3 play 1 time on Python Script Bootup

Fri Oct 18, 2013 4:44 am

I have the python script already running at boot. I just wanted to add the ability using my script, for an MP3 to play also at the time of boot within that script...

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Making MP3 play 1 time on Python Script Bootup

Fri Oct 18, 2013 7:18 am

Found this on http://raspberrypi.stackexchange.com/qu ... ith-python

I've checked that it works on an RPi with an mp3.

Code: Select all

import pygame
pygame.mixer.init()
pygame.mixer.music.load("myFile.mp3")
pygame.mixer.music.play()

But pygame needs an x-windowed environment which you might not have at bootup. So the solution with mpg321 might be better for your needs?
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Making MP3 play 1 time on Python Script Bootup

Fri Oct 18, 2013 8:12 am

DeeJay wrote:Found this on http://raspberrypi.stackexchange.com/qu ... ith-python

I've checked that it works on an RPi with an mp3.

Code: Select all

import pygame
pygame.mixer.init()
pygame.mixer.music.load("myFile.mp3")
pygame.mixer.music.play()

But pygame needs an x-windowed environment which you might not have at bootup. So the solution with mpg321 might be better for your needs?

....pygame does not need X-windows to run in my experience. I have a startup python program that I use to test my display boards and X isn't entered, but it uses pygame for the graphics.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “Beginners”