1

I'm trying to play a 1sec sound in loop using mplayer. When I run the code it crashes the raspberry os. I noticed that some times multiple instances of the mplayer processes were hanging in the task manager so I've added code to get the process id pidof and kill it.
But the process keep appearing multiple times, and raspberry keeps crashing after some loops.

Also, some times it seems the sound is overlapped (probably because of the multiple instances of the process)

import os
import time
import serial

# arduino stuff
ser = serial.Serial('/dev/ttyACM0', 9600)

while True:
    os.system('mplayer -really-quiet /home/pi/Desktop/sound.mp3 &')
    time.sleep(1.5)
    ser.write('1') # send a signal to arduino 
    a = os.popen('pidof mplayer').read()
    if(a != ''):
        os.system('sudo kill ' + str(a))

Note: I've tried to use other players, like mpg123, but the problem is exactly the same.

Rita Maia
  • 89
  • 1
  • 2
  • 11
  • Are you getting the IO response on the Arduino before it crashes? – Drewness Mar 10 '14 at 18:48
  • Ive tried to remove the arduino/serial communication code and the raspberry doesnt crashes anymore and the sound works great. But if i make anything else (even dragging the window) the raspberry pi gets extremely slow and the sounds starts to fail again. – Rita Maia Mar 10 '14 at 19:05

1 Answers1

0

If your sound file is any time longer than your sleep time of 1.5 seconds, it is natural that an endless number of processes builts up, because you are running the mplayer in the background, as can be seen from the trailing '&'.

Killing the processes is a bit of a hack. And it introduces the problem, that you open a file-like object with os.popen() which you never close.

Rather you should trim your sound file to the length you want to have it play and remove the '&' at the end of the command line. As an alternative to trimming the sound file you may find an option of mplayer limiting the time it plays the tune.

Harald
  • 4,575
  • 5
  • 33
  • 72
  • Ive removed the `&` and the `sleep` and the performance was better but after some minutes it crashes again. How can a raspberry pi crashes with just a sound loop and some arduino serial communication? – Rita Maia Mar 10 '14 at 19:53
  • See my latest edit. Did you remove the popen too, or at least close the file object that it opens. – Harald Mar 10 '14 at 19:54
  • Yes, i removed everything and for what it seems the sound is working as it should. Maybe the problem is when i start the serial communication? – Rita Maia Mar 10 '14 at 20:09
  • Also tried the arduino communication in a separated script and it works normal too. Only when are both executed at the same time the app crashes. – Rita Maia Mar 10 '14 at 20:13