Playing sounds and using buttons with Raspberry Pi - Problem

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
raspberrypibeginners
 
Posts: 4
Joined: Sat Dec 01, 2012 1:57 pm

Playing sounds and using buttons with Raspberry Pi - Problem

Post by raspberrypibeginners »

Hi guys,

I'm trying to run the example in "Playing sounds and using buttons with Raspberry Pi" and i'm finding that whenever i press the button it tries to play them MP3 over and over again within a few seconds and basically doesn't work. Any though?

raspberrypibeginners
 
Posts: 4
Joined: Sat Dec 01, 2012 1:57 pm

Re: Playing sounds and using buttons with Raspberry Pi - Problem

Post by raspberrypibeginners »

Hey did anyone have any thoughts on this?

The0001
 
Posts: 4
Joined: Thu Apr 25, 2013 4:39 pm

Re: Playing sounds and using buttons with Raspberry Pi - Problem

Post by The0001 »

I am also having problems with this example... I followed the tutorial verbatim since I just received my Pi. I was able to successfully run the program and the buttons do appear to register properly for each GPIO, but instead of hearing an MP3 play, I get an error message that says:
tcgetattr(): Inappropriate ioctl for device
glad.mp3: Invalid argument


Since I am completely new to linux, I have absolutely no idea how to begin to fix the MP3 not playing issue.
I have the same hardware that everyone else has. I followed the instructions on how to set up the audio. The MP3's worked (on my Windows laptop) and they are not very big in size. I put them in the same directory as the button program as instructed. I have headphones plugged into the audio jack. Any suggestions on what can I do to make the sound work?

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: Playing sounds and using buttons with Raspberry Pi - Problem

Post by tldr »

looks kind of like this is a problem in the process spawned to run mpg321.

which linux install are you using?

try

Code: Select all

mpg321 glad.mp3
from the shell. then try

Code: Select all

mpg321 glad.mp3&
show us the exact code you are running.

The0001
 
Posts: 4
Joined: Thu Apr 25, 2013 4:39 pm

Re: Playing sounds and using buttons with Raspberry Pi - Problem

Post by The0001 »

tldr,

The version reported when I boot up is Linux raspberrypi 3.2.27+
The tutorial is here: http://learn.adafruit.com/playing-sound ... spberry-pi

Here's the code, which is essentially the code copied from the tutorial with my mp3 filenames substituted in...

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 hailed.mp3 &')
        if ( GPIO.input(24) == False ):
                os.system('mpg321 glad.mp3 &')
        if ( GPIO.input(25)== False ):
                os.system('mpg321 braincells.mp3 &')
        sleep(0.1);
I followed the tutorial step-by-step, but got no sound when I press the GPIO buttons. :(
I also tried your suggestions and typed

Code: Select all

mpg321 glad.mp3

Code: Select all

mpg321 glad.mp3&

Code: Select all

sudo mpg321 glad.mp3

Code: Select all

sudo mpg321 glad.mp3&
even tried

Code: Select all

sudo mpg321 glad.mp3 &
just in case spaces make a difference. (I'm a total noob at Linux, btw.)
No dice.
Every time I try running mpg321, it spits out the message, "glad.mp3: Invalid argument"

At least the GPIO buttons work.
I think I need to find a simple example that tests the audio functionality of the Pi.

The0001
 
Posts: 4
Joined: Thu Apr 25, 2013 4:39 pm

Re: Playing sounds and using buttons with Raspberry Pi - Problem

Post by The0001 »

I got my audio to work...
I followed the guide at this site to test my audio: https://sites.google.com/site/semillero ... spberry-pi

I also learned that Midori doesn't allow file downloads. (Or at least it doesn't work for mp3's) Basically, my mp3's were invalid because I unsuccessfully tried downloading them with Midori. The filename shows up but upon closer inspection, its filesize was 0 bytes! (I only came to this realization after going through the audio test above and performing a command line download of their test audio file.)

I used a different browser to download mp3's and now the program works with sound. 8)

One minor potential issue with the code though...
I noticed that if you hold the button a little longer than your average button press, then the program seems to try to play multiple instances of the same sound clip simultaneously. The speed at which this appears to be controlled might be the "sleep" command? If set at 0.1, I assume it will loop through the polling loop quite fast and perhaps overwhelm the cpu?
Well, I seem to have locked up the Pi because I held the button down for a few seconds. In the amount of time it has taken me to type up this response, the Pi has not recovered, so it looks like it's time to yank the power.
I've got a lot to learn! Sigh.

dkj10738
 
Posts: 1
Joined: Tue May 28, 2013 3:42 am

Re: Playing sounds and using buttons with Raspberry Pi - Problem

Post by dkj10738 »

Help, I am having a similar problem. My audio plays fine from the CL, but as soon as I run the code from the tutorial, it starts playing one of the MP3 files incessantly. This happens even if I don't connect a ribbon cable to the GPIO pins. I am stuck.

User avatar
thirdrate
 
Posts: 5
Joined: Sun Mar 16, 2014 12:11 am

Re: Playing sounds and using buttons with Raspberry Pi - Pro

Post by thirdrate »

dkj10738 wrote:Help, I am having a similar problem. My audio plays fine from the CL, but as soon as I run the code from the tutorial, it starts playing one of the MP3 files incessantly. This happens even if I don't connect a ribbon cable to the GPIO pins. I am stuck.
So I'm a complete n00b at this but as far as I can tell the program actually works by playing the sound file when the circuit is broken by pressing the button... basically, pressing the button removes changes a given pin from receiving electricity to not receiving electricity. The status of not receiving electricity is what tells the program to play the music file.

If you're able to play the mp3 file fine from the command line but when you start the program it just starts playing instantly (and repeatedly) then it sounds like you might not have your circuit built right. Again, being completely new to this I may be way off track but here are a few things I noticed working on this myself:

1) if you got the opaque (not clear) breadboard with your starter kit, it is actually split two ways: long way for the middle part and short way for the outer (top and bottom) parts. There is a split along the channel that runs down the length of the board as far as the middle section is concerned, as well as a split between the left and right side where the red and blue lines on the top and bottom have a break in them. If your circuit is trying to pull voltage from one of the channels on the LEFT side of this break in the red/blue lines, but you actually plugged your ribbon/cobbler into the RIGHT side, then you need to use wires to bridge the gap between the left and right sides. A better solution IMO is just to make sure that all the wires/resistors that plug into the area between the blue and red lines are all on the same side of the middle split between these lines.

2) It made a difference for me which way the resistors were facing. Full disclosure, I might not have had them plugged in all the way to begin with, but I didn't get it to work until I had all the resistors facing the same direction and that means the brown stripe closer to the button and the gold stripe plugged into the voltage area (between red/blue lines)

3) this doesn't really apply to YOUR issue since you got the files to play on command line, but Midori, the built in browser on the GUI, does NOT download mp3 files correctly... this was my main issue. I went through the additional audio set-up recommended in one of the replies to the other issues but wanted to reiterate here that Midori browser does not download mp3 files correctly. I had to, from the command line, use the wget function: wget http:\\www.somewebsite.com\filename.mp3

Anyway, those were my big issues.

Lazerman
 
Posts: 1
Joined: Fri Jun 20, 2014 1:52 am

Re: Playing sounds and using buttons with Raspberry Pi - Pro

Post by Lazerman »

I may be able to help a little bit here. From the sound of it, mpg321 or the modprobe_bcm sound drivers may not have been installed correctly. Maybe try installing those again? And as far as orientation of resistors goes either way works just fine. Holding down one of the buttons for a few seconds and the pi trying to play the sound millions of times can be due either to switch bounce (http://en.wikipedia.org/wiki/Switch#Contact_bounce) or, if you had the & symbol after your sound in the code, the pi is actually calling that many instances of the sound every time the code is looped. (The & symbol pushes the process to the background allowing other processes to happen while the other process runs in the background). The Pi will also do this if you do not have a cable attached to the GPIO as interference such as cosmic rays or electromagnetic noise (http://en.wikipedia.org/wiki/Cosmic_noise) can cause the Pi's GPIO to think you are pushing buttons.

All of this being said I need help as well. I can run the script perfectly fine in the command line, but I am trying to run the script at boot and I am faced with a simmilar error.
tcgetattr(): Inappropriate ioctl for device hello.mp3: No such file or directory
I have this code in /etc/rc.local

Code: Select all

(sleep 10;sudo python sound_script.py) &

I have tried python3 and I have tried running the script from init.d with the same results. Any ideas?

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”