Subprocess

Lesson 9: Subprocess and Signal

An excellent website on subprocess is pymotw.com

In hindsight, I should have used the variable proc (for process) instead of pid (process id).

from fltk import *
import subprocess as sp
import signal

def playb_cb(wid):
    global pid
    if pid==0: #no music playing already
        pid = sp.Popen(['vlc', '--intf', 'dummy', 'zapac.mp3']) 
        # --intf dummy (interface dummy to disable vlc gui)

def signal_cb(wid, s):
    global pid
    if pid!=0:  # already playing
        pid.send_signal(s)
        if s==signal.SIGTERM:
            pid = 0


def close_win(wid):
    if pid!=0:
        pid.send_signal(signal.SIGTERM)
    wid.hide()

pid=0
win=Fl_Window(300,300)

win.begin()
playb = Fl_Button(0,0,70,40,'@>')
stopb = Fl_Button(70,0,70,40,'@square')
pauseb = Fl_Button(140,0,70,40,'@||')
resumeb = Fl_Button(210,0,70,40,'@|>')
win.end()

playb.callback(playb_cb)
stopb.callback(signal_cb, signal.SIGTERM)
pauseb.callback(signal_cb, signal.SIGSTOP)
resumeb.callback(signal_cb,signal.SIGCONT)
win.callback(close_win)

win.show()
Fl.run()

Another option is to use the vlc python API. Install the vlc module with the following command:

pip install python-vlc

or in Debian based Linux systems (as root)

apt install python3-vlc