I'm not a programmer but i'm trying to build a audiobook for my wife's students .
I've linked a button matrix board with a Arduino connected to the PI .
Everything works fine but i've a last bug, i have no idea how to fix it.
If the children press a button, the pi start to play a audiobook.
If i press on the pause button ( Pi receive "L","M","T","U" from arduino) the paus works, if they press again, the pi continue to play.
I have a issue, if the pi is not playing a file, but we press on pause the the program crash.
I assume the pi is trying to send the command to omxplayer, but it's not actif.
How can i check if omxplayer is playing before sending the pause command ?
issue should be here :
Code: Select all
if input in ("L","M","T","U") :
print input
print ("Pause")
player.stdin.write("p")see full code bellow
Code: Select all
# Imports
import time
import RPi.GPIO as GPIO
import os
import serial
import shlex
import subprocess
from subprocess import Popen
#definition du serial
ser = serial.Serial('/dev/ttyACM0', 9600)
killomx = "sudo killall -s 9 omxplayer.bin"
os.system(killomx)
player = subprocess.Popen(["omxplayer","--vol","-1000","hello.mp3"],stdin=subprocess.PIPE)
while True:
if ser.inWaiting()>0:
input = ser.read() #on lit le port serial
print input
if input in ("1","2","9","A") :
print input
print ("histoire1")
killomx = "sudo killall -s 9 omxplayer.bin"
#sortie = "alsa"
#arg = shlex.split('omxplayer -o alsa livre1.mp3')
#print(arg)
os.system(killomx)
player = subprocess.Popen(["omxplayer","livre1.mp3"],stdin=subprocess.PIPE)
#print player
if input in ("3","4","B","C") :
print input
print ("histoire2")
killomx = "sudo killall -s 9 omxplayer.bin"
#sortie = "alsa"
#arg = shlex.split('omxplayer -o alsa livre1.mp3')
#print(arg)
os.system(killomx)
player = subprocess.Popen(["omxplayer","livre2.mp3"],stdin=subprocess.PIPE)
#print player
if input in ("5","6","D","E") :
print input
print ("histoire3")
killomx = "sudo killall -s 9 omxplayer.bin"
#sortie = "alsa"
#arg = shlex.split('omxplayer -o alsa livre1.mp3')
#print(arg)
os.system(killomx)
player = subprocess.Popen(["omxplayer","livre3.mp3"],stdin=subprocess.PIPE)
#print player
if input in ("7","8","F","G") :
print input
print ("histoire4")
killomx = "sudo killall -s 9 omxplayer.bin"
#sortie = "alsa"
#arg = shlex.split('omxplayer -o alsa livre1.mp3')
#print(arg)
os.system(killomx)
player = subprocess.Popen(["omxplayer","livre4.mp3"],stdin=subprocess.PIPE)
#print player
if input in ("H","I","P","Q") :
print input
print ("histoire5")
killomx = "sudo killall -s 9 omxplayer.bin"
#sortie = "alsa"
#arg = shlex.split('omxplayer -o alsa livre1.mp3')
#print(arg)
os.system(killomx)
player = subprocess.Popen(["omxplayer","livre5.mp3"],stdin=subprocess.PIPE)
#print player
if input in ("J","K","R","S") :
print input
print ("histoire6")
killomx = "sudo killall -s 9 omxplayer.bin"
#sortie = "alsa"
#arg = shlex.split('omxplayer -o alsa livre1.mp3')
#print(arg)
os.system(killomx)
player = subprocess.Popen(["omxplayer","livre6.mp3"],stdin=subprocess.PIPE)
#print player
if input in ("L","M","T","U") :
print input
print ("Pause")
player.stdin.write("p")
if input in ("N","O","V","W") :
print input
print ("Thank you for listening")
killomx = "sudo killall -s 9 omxplayer.bin"
os.system(killomx)
if (input == 'X') :
print ("Vol -")
player.stdin.write("-")
if (input == 'Y') :
print ("Vol +")
player.stdin.write("+")
time.sleep(0.5)