3

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').

2

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)
  • I do get an error if i go right to far instead of starting over it says that i am out of range, kinda strange but works great! – Reese Houseknecht Apr 23 at 12:37
  • Your right - should be if file>len(files)-1 I've corrected it. – CoderMike Apr 23 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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