1

So here I have created a loop that lists off each file in a folder and plays it using this python code:

        global shuffleVar
        listOfFiles = os.listdir('/home/pi/mysounds/')  
        pattern = "*.mp3"  
        for entry in listOfFiles:  
            if fnmatch.fnmatch(entry, pattern):
                print (entry)
                os.system('killall omxplayer.bin')
                shuffleVar = "omxplayer -o local -I --vol 0 /home/pi/mysounds/" + (entry) + " &"
                os.system(shuffleVar)

To break this down listOfFiles shows the directory of the music that needs tobe shuffled, pattern searches for only mp3 files, and the loop goes through each file and then makes a string to run omxplayer which is executed in os.system. Now before going to the next song i want to be able to have keyboard and mouse inputs to either skip ahead or turn up or down volume. For this i want to use the following loop

                #track keyboard and mouse
                x_max = 2180 #defines largest position mouse can go (19th LED)
                x_min = 0 #defines smallest position mouse can go (9th LED)
                y_max = 2180
                y_min = 0

                async def print_events(device):
                    async for event in device.async_read_loop():
                        if event.type == ecodes.EV_KEY: #if the event is a button click
                            c = categorize(event)
                            if c.keystate == c.key_down: #only reading key for down press not up
                                print(c.keycode)
                                if ("KEY_RIGHT" in c.keycode):
                                    return

                if (globals()['correct_event'] == 1):
                    print("event determination is 1")
                    for device in keyboardEvent, mouseEvent:
                        asyncio.ensure_future(print_events(device))
                if (globals()['correct_event'] == 2):
                    print("event determination is 2")
                    asyncio.ensure_future(print_events(keyboardEvent))
                loop = asyncio.get_event_loop()
                loop.run_forever()

now here if i press KEY_RIGHT i want to exit the listening to keyboard and mouse loop and have the for loop go to the next song. I have return after it but im not sure where i will return to. What do i do to exit the track keyboard and mouse loop and have the next for loop run through?

4
  • Might be easier to answer if you post your complete code. You might find the omxplayer wrapper easier to use python-omxplayer-wrapper.readthedocs.io/en/latest
    – CoderMike
    Commented Apr 14, 2019 at 18:01
  • if i use that how can i say if KEY_RIGHT play the next song? Commented Apr 14, 2019 at 18:04
  • You still need to detect your key press as you are doing.
    – CoderMike
    Commented Apr 14, 2019 at 18:17
  • @CoderMike Just at a first glance: the link you have given for omxplayer-wrapper installs with pip but uses python3.
    – Ingo
    Commented Apr 14, 2019 at 19:07

2 Answers 2

1

Certainly not a permanent change but by creating a separate file to be called i can control the keyboard when a song is playing, and to exit the async loop i use loop.stop(). This would work in my main code which has another async loop in it else where but i always get an error saying process terminated but pending! Here is my code that works!

import os
import fnmatch #used to list shuffle play files
import sys, termios, tty, time, serial #imports all required items
import serial.tools.list_ports
import evdev
import asyncio
from evdev import ecodes, categorize, KeyEvent, InputDevice

while (1 == 1): #this will be set to a click or esc exit inside the loop to end shuffle
    #this will list all files into an array
    global shuffleVar
    listOfFiles = os.listdir('/home/pi/mysounds/')  
    pattern = "*.mp3"  
    print(listOfFiles)
    for entry in listOfFiles:  
        if fnmatch.fnmatch(entry, pattern):
        print (entry)
        os.system('killall omxplayer.bin')
        shuffleVar = "omxplayer -o local -I --vol 0 /home/pi/mysounds/" + (entry) + " &"
        os.system(shuffleVar)

        #track keyboard and mouse
        x_max = 2180 #defines largest position mouse can go (19th LED)
        x_min = 0 #defines smallest position mouse can go (9th LED)
        y_max = 2180
        y_min = 0
        keyboardEvent = evdev.InputDevice('/dev/input/event0') #setting keyboard to USB port
        mouseEvent = evdev.InputDevice('/dev/input/event1') #setting mouse to USB port

        async def print_events2(device):
            async for event in device.async_read_loop():
                if event.type == ecodes.EV_KEY: #if the event is a button click
                    c = categorize(event)
                    if c.keystate == c.key_down: #only reading key for down press not up
                        print(c.keycode)
                        if ("KEY_RIGHT" in c.keycode):
                            print("next song")
                            loop.stop()

            for device in globals()['keyboardEvent'], globals()['mouseEvent']:
                asyncio.ensure_future(print_events2(device))
        loop = asyncio.get_event_loop()
        loop.run_forever()
1

I use this "quick and dirty" bash script to shuffle play audio files in a given directory on my Pi. Just do CTRL-C once to move to the next file and twice to exit the shuffle play.

#!/bin/bash

cd /media/wd/audiovideo/audio/

while [ 1 -gt 0 ]
do
FILE=$(ls | shuf -n 1)
echo $FILE
omxplayer $FILE
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.