I had a brainstorm and took my .lua back to the online resources and changed my wording requesting the showing of the image and the playing of the .mp3 to help separate the actions and control them. It freaking worked!!!
I now have the script simultaneously displaying one random image and playing the corresponding .mp3!
Now all I need is a final pause, and then an automated close of the conky widget. I tried to get working code from online but it was no good. I was given this:
Code: Select all
-- Function to check if playerctl is running
function isPlayerctlRunning()
local handle = io.popen("playerctl status 2>/dev/null")
local result = handle:read("*a")
handle:close()
return result ~= ""
end
-- Monitor for media playback using playerctl
while isPlayerctlRunning() do
os.execute("sleep 1")
end
-- Set here the PATH to conky
local eqconky = "/home/logansfury/.conky/Biohazzard/joke.conf"
-- Search for the joke widget using wmctrl
local handle = io.popen("wmctrl -l | grep joke")
local wmctrl_output = handle:read("*a")
handle:close()
-- Get PID of conky
local handle_ps = io.popen("ps aux | grep \"" .. eqconky .. "\" | grep -v grep | awk '{print $2}'")
local pid = handle_ps:read("*a")
handle_ps:close()
-- Kill the process with the obtained PID
os.execute("kill " .. pid)
print("joke widget closed")
end
I then did some experimenting with bash scripts and found that playerctl does NOT detect mpg123 running.
Here is the current .lua, correctly executing image and sound:
Code: Select all
require 'cairo'
require 'imlib2'
home_path = os.getenv('HOME')
math.randomseed(os.time()) -- Seed the random number generator with the current time
local mp3_played = false
local random_number = math.random(1, 51)
function fDrawImage(cr, path, xx, yy, ww, hh, arc)
local img = cairo_image_surface_create_from_png(path)
local w_img = cairo_image_surface_get_width(img)
local h_img = cairo_image_surface_get_height(img)
cairo_translate(cr, xx, yy)
if arc then
cairo_rotate(cr, arc)
end
cairo_scale(cr, ww / w_img, hh / h_img)
cairo_set_source_surface(cr, img, -w_img / 2, -h_img / 2)
cairo_paint(cr)
cairo_surface_destroy(img)
collectgarbage()
end
function conky_main()
if conky_window == nil then
return
end
local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
local cr = cairo_create(cs)
-- Draw the image
print("Drawing image...") -- Debug statement
fDrawImage(cr, home_path .. '/Pictures/microsoft_jokes/' .. tostring(random_number) .. '.png', 162, 107, 324, 214)
print("Image drawn.") -- Debug statement
cairo_surface_destroy(cs)
cairo_destroy(cr)
-- Play the MP3
if not mp3_played then
print("Playing MP3...") -- Debug statement
os.execute("mpg123 " .. home_path .. "/Music/eyegor/" .. tostring(random_number) .. ".mp3 &")
mp3_played = true
print("MP3 played.") -- Debug statement
end
end
Thanks for reading