4

on my raspberry pi i have a speaker wired connected via aux cable to the 3.5mm audio jack. My first question ishow can i get rid of the noise coming out of my 3.5mm audio jack when keys are pressed or processes are running. And more importantly if i play a song with omxplayer using

os.system('omxplayer -o local -I --vol 0 /home/pi/mysounds/sound.mp3 &')

how can i use another os.system command to turn the volume up or down (this is all written in python so that when i press a key it will change the volume').

Reese Houseknecht
  • 105
  • 1
  • 1
  • 10
  • 2
    You may want to have a look at omxplayer wrapper (https://python-omxplayer-wrapper.readthedocs.io/en/latest/). It gives you a lot of options for controlling omxplayer, including volume – Dirk Apr 15 '19 at 17:10
  • can you provide some code as an example – Reese Houseknecht Apr 15 '19 at 17:49
  • I have never used it myself. I would study the docs and the examples on github (https://github.com/willprice/python-omxplayer-wrapper) – Dirk Apr 15 '19 at 18:05
  • One example https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=237688#p1453558 – CoderMike Apr 15 '19 at 18:59

1 Answers1

3

This is how I would change the volume as well as left/right playlist:

import evdev,time,glob
from omxplayer.player import OMXPlayer

# pip3 install omxplayer-wrapper

def playerExit(code):
    print('exit',code)
    global playing
    playing=False

def playFile(file):
    global player,playing
    if player==None:
        player=OMXPlayer(file)
        player.set_volume(volume)
        player.exitEvent += lambda _, exit_code: playerExit(exit_code)
    else:
        player.load(file)
    print('Playing:',file)
    playing=True

def quitPlayer():
    if player!=None:
        player.quit()

def getDevice():
    for fn in evdev.list_devices():
        device = evdev.InputDevice(fn)
        caps = device.capabilities()
        if evdev.events.EV_KEY in caps:
            if evdev.ecodes.KEY_1 in caps[evdev.events.EV_KEY]:
                return device

    raise IOError('No keyboard found')

dev = getDevice()
print(dev)
#print(dev.capabilities(verbose=True))
player = None
files = glob.glob('/home/pi/Music/*.mp3')

print(files)
print(len(files))
file=0
volume=0.2

for ev in dev.read_loop():
    #print(ev)
    if ev.type == evdev.ecodes.EV_KEY:
        #print(evdev.categorize(ev))
        active=dev.active_keys()
        if evdev.ecodes.KEY_LEFTCTRL in active and (evdev.ecodes.KEY_X in active or evdev.ecodes.KEY_C in active):
            quitPlayer()
            print('Exit')
            break

        if ev.code ==  evdev.ecodes.KEY_LEFT and ev.value == 1:
            print('left')
            file-=1
            if file<0:
                file=len(files)
            playFile(files[file])

        if ev.code ==  evdev.ecodes.KEY_RIGHT and ev.value == 1:
            print('right')
            file+=1
            if file>len(files)-1:
                file=0
            playFile(files[file])

        if ev.code ==  evdev.ecodes.KEY_UP and ev.value == 1:
            volume+=0.1
            if volume>1:
                volume=1
            print('up',volume)                
            if player!=None:
                player.set_volume(volume)

        if ev.code ==  evdev.ecodes.KEY_DOWN and ev.value == 1:
            volume-=0.1
            if volume<0:
                volume=0
            print('down',volume)                
            if player!=None:
                player.set_volume(volume)
CoderMike
  • 6,982
  • 1
  • 10
  • 15