karlzre
Posts: 20
Joined: Sat Dec 01, 2018 10:11 am

Audiobook - OMXplayer pause issue

Sun Sep 01, 2019 6:51 pm

Hi,

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)
              
Thx

User avatar
rpiMike
Posts: 1340
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Audiobook - OMXplayer pause issue

Mon Sep 02, 2019 6:18 am

You might find the omxplayer wrapper helpful:

https://python-omxplayer-wrapper.readth ... en/latest/

jehutting
Posts: 143
Joined: Sun Feb 15, 2015 8:37 am
Location: The Netherlands

Re: Audiobook - OMXplayer pause issue

Mon Sep 02, 2019 10:39 am

Use subprocess poll() to check if the player is still alive.
Poll() returns None when the process hasn’t terminated yet.

Code: Select all

        if input in ("L","M","T","U") :
            print input
  	    if player.poll() is None:
	        print ("Pause")
		player.stdin.write("p")

Do the same for the volume + and - code.

karlzre
Posts: 20
Joined: Sat Dec 01, 2018 10:11 am

Re: Audiobook - OMXplayer pause issue

Mon Sep 02, 2019 2:22 pm

Thx,
will test it.

karlzre
Posts: 20
Joined: Sat Dec 01, 2018 10:11 am

Re: Audiobook - OMXplayer pause issue

Mon Sep 02, 2019 11:14 pm

Hi,

It work perfect :-)

I just add a lcd screen to the pi, so it's more user friendly.

i can show which book it's reading.
I have a new "issue",

if i press pause is it possible to show again the previous text with the same formatting.


I mean if i press pause, it show pause, if i press pause again it continue reading and it show the book's it's reading.
it looks to works, but if i press pause, it show pause and pause the book and unpause and continue reading a few second later.

i think the keypad is a little bit too sensitive, then the script receive 2 time the input and toggle from pause to unpause.

How can i make it less sensitive ?

Code: Select all

if input in ("L","M","T","U") :
            print input
            if player.poll() is None:
                if pausestat == 0:
                    print ("Pause")
                    lcd.clear()
                    lcd.cursor_pos = (0, 4)
                    lcd.write_string(u'Pause')
                    player.stdin.write("p")
                    pausestat = 1
                    time.sleep(2)
                    input = "Z"
                if pausestat == 1:
                    print ("UnPause")
                    player.stdin.write("p")
                    lcd.clear()
                    lcd.cursor_pos = (0, col0)
                    lcd.write_string(affiche1)
                    lcd.cursor_pos = (1, col1)
                    lcd.write_string(affiche2)
                    pausestat = 0
                    time.sleep(2)
                    input = "Z"    
Here is the full code

Code: Select all

# Imports
import time
import RPi.GPIO as GPIO
import os
import serial
import shlex
import subprocess
from subprocess import Popen
from RPLCD.gpio import CharLCD

#definition du serial
ser = serial.Serial('/dev/ttyACM0', 9600)
pausestat = 0
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])
killomx = "sudo killall -s 9 omxplayer.bin"
os.system(killomx)
player = subprocess.Popen(["omxplayer","--vol","-1000","hello.mp3"],stdin=subprocess.PIPE)
lcd.cursor_pos = (0, 4)
lcd.write_string(u'Bienvue')
lcd.cursor_pos = (1, 3)
lcd.write_string(u'Audiobook')
time.sleep(6)
lcd.clear()
lcd.cursor_pos = (0, 4)
lcd.write_string(u'Choisis')
lcd.cursor_pos = (1, 3)
lcd.write_string(u'une histoire')
while True:
    if ser.inWaiting()>0:
        input = ser.read() #on lit le port serial
        print input
        if input in ("1","2","9","A") :
            lcd.clear()
            print input
            print ("histoire1")
            col0 = 4
            col1 = 0
            lcd.cursor_pos = (0, col0)
            affiche1 = "Doucette"
            affiche2 = ""
            lcd.write_string(affiche1)
            killomx = "sudo killall -s 9 omxplayer.bin"
            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")
            affiche1 = "Grand"
            affiche2 = "Chambardement"
            lcd.clear()
            col0 = 4
            col1 = 2
            lcd.cursor_pos = (0, col0)
            lcd.write_string(affiche1)
            lcd.cursor_pos = (1, col1)
            lcd.write_string(affiche2)
            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")
            lcd.clear()
            affiche1 = "Hansel"
            affiche2 = "et Gretel"
            col0 = 5
            col1 = 4
            lcd.cursor_pos = (0, col0)
            lcd.write_string(affiche1)
            lcd.cursor_pos = (1, col1)
            lcd.write_string(affiche2)
            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")
            lcd.clear()
            affiche1 = "La"
            affiche2 = "Colocataire"
            col0 = 7
            col1 = 2
            lcd.cursor_pos = (0, col0)
            lcd.write_string(affiche1)
            lcd.cursor_pos = (1, col1)
            lcd.write_string(affiche2)
            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")
            lcd.clear()
            affiche1 = "Plume"
            affiche2 = ""
            col0 = 5
            col1 = 4
            lcd.cursor_pos = (0, col0)
            lcd.write_string(affiche1)
            lcd.cursor_pos = (1, col1)
            lcd.write_string(affiche2)
            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")
            lcd.clear()
            affiche1 = "Mon petit"
            affiche2 = "Poussin vert"
            col0 = 3
            col1 = 2
            lcd.cursor_pos = (0, col0)
            lcd.write_string(affiche1)
            lcd.cursor_pos = (1, col1)
            lcd.write_string(affiche2)
            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
            if player.poll() is None:
                if pausestat == 0:
                    print ("Pause")
                    lcd.clear()
                    lcd.cursor_pos = (0, 4)
                    lcd.write_string(u'Pause')
                    player.stdin.write("p")
                    pausestat = 1
                    time.sleep(2)
                    input = "Z"
                if pausestat == 1:
                    print ("UnPause")
                    player.stdin.write("p")
                    lcd.clear()
                    lcd.cursor_pos = (0, col0)
                    lcd.write_string(affiche1)
                    lcd.cursor_pos = (1, col1)
                    lcd.write_string(affiche2)
                    pausestat = 0
                    time.sleep(2)
                    input = "Z"
        if input in ("N","O","V","W") :
            print input
            print ("Thank you for listening")
            lcd.clear()
            lcd.cursor_pos = (0, 4)
            lcd.write_string(u'Stop')
            killomx = "sudo killall -s 9 omxplayer.bin"
            os.system(killomx)
            time.sleep(3)
            lcd.clear()
            lcd.cursor_pos = (0, 4)
            lcd.write_string(u'Choisis')
            lcd.cursor_pos = (1, 3)
            lcd.write_string(u'une histoire')
        if (input == 'X') :
            if player.poll() is None:
                print ("Vol -")
                lcd.clear()
                lcd.cursor_pos = (0, 4)
                lcd.write_string(u'Vol -')
                player.stdin.write("-")
                time.sleep(1)
                lcd.clear()
                lcd.cursor_pos = (0, col0)
                lcd.write_string(affiche1)
                lcd.cursor_pos = (1, col1)
                lcd.write_string(affiche2)
        if (input == 'Y') :
            if player.poll() is None:
                print ("Vol +")
                lcd.clear()
                lcd.cursor_pos = (0, 4)
                lcd.write_string(u'Vol +')
                player.stdin.write("+")
                time.sleep(1)
                lcd.clear()
                lcd.cursor_pos = (0, col0)
                lcd.write_string(affiche1)
                lcd.cursor_pos = (1, col1)
                lcd.write_string(affiche2)

        time.sleep(0.5)
        
        
        
        
        
        
        


Return to “Python”